39
// getIdAsString extracts the field with the specified tag from the json data
40
// and returns it converted to a string.
41
func getIdAsString(b []byte, tag string) (string, error) {
42
var out map[string]interface{}
43
if err := json.Unmarshal(b, &out); err != nil {
46
if val, ok := out[tag]; !ok {
49
if floatVal, ok := val.(float64); ok {
50
return fmt.Sprint(int(floatVal)), nil
52
return fmt.Sprint(val), nil
46
58
// appendJSON marshals the given attribute value and appends it as an encoded value to the given json data.
47
59
// The newly encode (attr, value) is inserted just before the closing "}" in the json data.
48
60
func appendJSON(data []byte, attr string, value interface{}) ([]byte, error) {
80
91
id := convertId(entity.Id)
81
return appendJSON(data, "Id", id)
92
return appendJSON(data, idTag, id)
84
95
type jsonFlavorDetail FlavorDetail
86
97
func (flavorDetail *FlavorDetail) UnmarshalJSON(b []byte) error {
87
98
var jfd jsonFlavorDetail = jsonFlavorDetail(*flavorDetail)
88
if err := json.Unmarshal(b, &jfd); err != nil {
92
if err := json.Unmarshal(b, &id); err != nil {
100
if err = json.Unmarshal(b, &jfd); err != nil {
103
if jfd.Id, err = getIdAsString(b, idTag); err != nil {
96
106
*flavorDetail = FlavorDetail(jfd)
106
116
id := convertId(flavorDetail.Id)
107
return appendJSON(data, "Id", id)
117
return appendJSON(data, idTag, id)
110
120
type jsonServerDetail ServerDetail
112
122
func (serverDetail *ServerDetail) UnmarshalJSON(b []byte) error {
113
123
var jsd jsonServerDetail = jsonServerDetail(*serverDetail)
114
if err := json.Unmarshal(b, &jsd); err != nil {
118
if err := json.Unmarshal(b, &id); err != nil {
125
if err = json.Unmarshal(b, &jsd); err != nil {
128
if jsd.Id, err = getIdAsString(b, idTag); err != nil {
122
131
*serverDetail = ServerDetail(jsd)
132
141
id := convertId(serverDetail.Id)
133
return appendJSON(data, "Id", id)
136
type genericInstanceId struct {
137
InstanceId interface{} `json:"instance_id"`
140
func (id genericInstanceId) String() string {
141
if id.InstanceId == nil {
144
if fid, ok := id.InstanceId.(float64); ok {
145
return fmt.Sprint(int(fid))
147
return fmt.Sprint(id.InstanceId)
142
return appendJSON(data, idTag, id)
150
145
type jsonFloatingIP FloatingIP
152
147
func (floatingIP *FloatingIP) UnmarshalJSON(b []byte) error {
153
148
var jfip jsonFloatingIP = jsonFloatingIP(*floatingIP)
154
if err := json.Unmarshal(b, &jfip); err != nil {
157
var id genericInstanceId
158
if err := json.Unmarshal(b, &id); err != nil {
161
instId := id.String()
150
if err = json.Unmarshal(b, &jfip); err != nil {
153
if instIdStr, err := getIdAsString(b, instanceIdTag); err != nil {
155
} else if instIdStr != "" {
164
157
jfip.InstanceId = &strId
159
if jfip.Id, err = getIdAsString(b, idTag); err != nil {
166
162
*floatingIP = FloatingIP(jfip)
172
id := convertId(floatingIP.Id)
173
data, err = appendJSON(data, idTag, id)
176
177
if floatingIP.InstanceId == nil {
179
id := convertId(*floatingIP.InstanceId)
180
return appendJSON(data, "instance_id", id)
180
instId := convertId(*floatingIP.InstanceId)
181
return appendJSON(data, instanceIdTag, instId)
184
type jsonSecurityGroup SecurityGroup
186
func (securityGroup *SecurityGroup) UnmarshalJSON(b []byte) error {
187
var jsg jsonSecurityGroup = jsonSecurityGroup(*securityGroup)
189
if err = json.Unmarshal(b, &jsg); err != nil {
192
if jsg.Id, err = getIdAsString(b, idTag); err != nil {
195
*securityGroup = SecurityGroup(jsg)
199
func (securityGroup SecurityGroup) MarshalJSON() ([]byte, error) {
200
var jsg jsonSecurityGroup = jsonSecurityGroup(securityGroup)
201
data, err := json.Marshal(&jsg)
205
id := convertId(securityGroup.Id)
206
return appendJSON(data, idTag, id)
209
type jsonSecurityGroupRule SecurityGroupRule
211
func (securityGroupRule *SecurityGroupRule) UnmarshalJSON(b []byte) error {
212
var jsgr jsonSecurityGroupRule = jsonSecurityGroupRule(*securityGroupRule)
214
if err = json.Unmarshal(b, &jsgr); err != nil {
217
if jsgr.Id, err = getIdAsString(b, idTag); err != nil {
220
if jsgr.ParentGroupId, err = getIdAsString(b, parentGroupIdTag); err != nil {
223
*securityGroupRule = SecurityGroupRule(jsgr)
227
func (securityGroupRule SecurityGroupRule) MarshalJSON() ([]byte, error) {
228
var jsgr jsonSecurityGroupRule = jsonSecurityGroupRule(securityGroupRule)
229
data, err := json.Marshal(&jsgr)
233
id := convertId(securityGroupRule.Id)
234
data, err = appendJSON(data, idTag, id)
238
if securityGroupRule.ParentGroupId == "" {
241
id = convertId(securityGroupRule.ParentGroupId)
242
return appendJSON(data, parentGroupIdTag, id)
245
type jsonRuleInfo RuleInfo
247
func (ruleInfo *RuleInfo) UnmarshalJSON(b []byte) error {
248
var jri jsonRuleInfo = jsonRuleInfo(*ruleInfo)
250
if err = json.Unmarshal(b, &jri); err != nil {
253
if jri.ParentGroupId, err = getIdAsString(b, parentGroupIdTag); err != nil {
256
if groupId, err := getIdAsString(b, groupIdTag); err != nil {
258
} else if groupId != "" {
262
*ruleInfo = RuleInfo(jri)
266
func (ruleInfo RuleInfo) MarshalJSON() ([]byte, error) {
267
var jri jsonRuleInfo = jsonRuleInfo(ruleInfo)
268
data, err := json.Marshal(&jri)
272
id := convertId(ruleInfo.ParentGroupId)
273
data, err = appendJSON(data, parentGroupIdTag, id)
277
if ruleInfo.GroupId == nil {
280
id = convertId(*ruleInfo.GroupId)
281
return appendJSON(data, groupIdTag, id)