Skip to content

Commit 1c5b46e

Browse files
committed
Change details field encoding to match cloudstack expectations
How the details field must be encoded changes depending on the command implementation. Some commands allow arbitrary keys and some other commands require only 2 keys to exist ('key' and 'value') and allow a list of such key value pairs as the details field. I wasn't able to find anything in the documentation that would make it clear which encoding was required for each command and had to look at cloudstack's code to figure this out. Here is a list of the commands singled out in this commit and references to the required encoding in cloudstack's code: addGuestOs https://github.com/apache/cloudstack/blob/87c43501608a1df72a2f01ed17a522233e6617b0/api/src/main/java/org/apache/cloudstack/api/command/admin/guest/AddGuestOsCmd.java#L88-L89 updateGuestOs https://github.com/apache/cloudstack/blob/87c43501608a1df72a2f01ed17a522233e6617b0/api/src/main/java/org/apache/cloudstack/api/command/admin/guest/UpdateGuestOsCmd.java#L80-L81 addImageStore https://github.com/apache/cloudstack/blob/87c43501608a1df72a2f01ed17a522233e6617b0/api/src/main/java/org/apache/cloudstack/api/command/admin/storage/AddImageStoreCmd.java#L90-L91 createSecondaryStagingStore https://github.com/apache/cloudstack/blob/87c43501608a1df72a2f01ed17a522233e6617b0/api/src/main/java/org/apache/cloudstack/api/command/admin/storage/CreateSecondaryStagingStoreCmd.java#L80 updateCloudToUseObjectStore https://github.com/apache/cloudstack/blob/87c43501608a1df72a2f01ed17a522233e6617b0/api/src/main/java/org/apache/cloudstack/api/command/admin/storage/UpdateCloudToUseObjectStoreCmd.java#L84-L85 addResourceDetail https://github.com/apache/cloudstack/blob/87c43501608a1df72a2f01ed17a522233e6617b0/api/src/main/java/org/apache/cloudstack/api/command/user/volume/AddResourceDetailCmd.java#L68-L69 updateZone https://github.com/apache/cloudstack/blob/87c43501608a1df72a2f01ed17a522233e6617b0/server/src/main/java/com/cloud/configuration/ConfigurationManagerImpl.java#L1866-L1867
1 parent 47f3e6c commit 1c5b46e

5 files changed

Lines changed: 40 additions & 10 deletions

File tree

cloudstack/GuestOSService.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ func (p *AddGuestOsParams) toURLValues() url.Values {
3636
if v, found := p.p["details"]; found {
3737
i := 0
3838
for k, vv := range v.(map[string]string) {
39-
u.Set(fmt.Sprintf("details[%d].%s", i, k), vv)
39+
u.Set(fmt.Sprintf("details[%d].key", i), k)
40+
u.Set(fmt.Sprintf("details[%d].value", i), vv)
4041
i++
4142
}
4243
}
@@ -969,7 +970,8 @@ func (p *UpdateGuestOsParams) toURLValues() url.Values {
969970
if v, found := p.p["details"]; found {
970971
i := 0
971972
for k, vv := range v.(map[string]string) {
972-
u.Set(fmt.Sprintf("details[%d].%s", i, k), vv)
973+
u.Set(fmt.Sprintf("details[%d].key", i), k)
974+
u.Set(fmt.Sprintf("details[%d].value", i), vv)
973975
i++
974976
}
975977
}

cloudstack/ImageStoreService.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ func (p *AddImageStoreParams) toURLValues() url.Values {
3636
if v, found := p.p["details"]; found {
3737
i := 0
3838
for k, vv := range v.(map[string]string) {
39-
u.Set(fmt.Sprintf("details[%d].%s", i, k), vv)
39+
u.Set(fmt.Sprintf("details[%d].key", i), k)
40+
u.Set(fmt.Sprintf("details[%d].value", i), vv)
4041
i++
4142
}
4243
}
@@ -323,7 +324,8 @@ func (p *CreateSecondaryStagingStoreParams) toURLValues() url.Values {
323324
if v, found := p.p["details"]; found {
324325
i := 0
325326
for k, vv := range v.(map[string]string) {
326-
u.Set(fmt.Sprintf("details[%d].%s", i, k), vv)
327+
u.Set(fmt.Sprintf("details[%d].key", i), k)
328+
u.Set(fmt.Sprintf("details[%d].value", i), vv)
327329
i++
328330
}
329331
}
@@ -1045,7 +1047,8 @@ func (p *UpdateCloudToUseObjectStoreParams) toURLValues() url.Values {
10451047
if v, found := p.p["details"]; found {
10461048
i := 0
10471049
for k, vv := range v.(map[string]string) {
1048-
u.Set(fmt.Sprintf("details[%d].%s", i, k), vv)
1050+
u.Set(fmt.Sprintf("details[%d].key", i), k)
1051+
u.Set(fmt.Sprintf("details[%d].value", i), vv)
10491052
i++
10501053
}
10511054
}

cloudstack/ResourcemetadataService.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ func (p *AddResourceDetailParams) toURLValues() url.Values {
3535
if v, found := p.p["details"]; found {
3636
i := 0
3737
for k, vv := range v.(map[string]string) {
38-
u.Set(fmt.Sprintf("details[%d].%s", i, k), vv)
38+
u.Set(fmt.Sprintf("details[%d].key", i), k)
39+
u.Set(fmt.Sprintf("details[%d].value", i), vv)
3940
i++
4041
}
4142
}

cloudstack/ZoneService.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1109,7 +1109,8 @@ func (p *UpdateZoneParams) toURLValues() url.Values {
11091109
if v, found := p.p["details"]; found {
11101110
i := 0
11111111
for k, vv := range v.(map[string]string) {
1112-
u.Set(fmt.Sprintf("details[%d].%s", i, k), vv)
1112+
u.Set(fmt.Sprintf("details[%d].key", i), k)
1113+
u.Set(fmt.Sprintf("details[%d].value", i), vv)
11131114
i++
11141115
}
11151116
}

generate/generate.go

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,7 @@ func (s *service) generateToURLValuesFunc(a *API) {
830830
pn(" }")
831831
for _, ap := range a.Params {
832832
pn(" if v, found := p.p[\"%s\"]; found {", ap.Name)
833-
s.generateConvertCode(ap.Name, mapType(ap.Type))
833+
s.generateConvertCode(ap.Name, a.Name, mapType(ap.Type))
834834
pn(" }")
835835
}
836836
pn(" return u")
@@ -839,7 +839,7 @@ func (s *service) generateToURLValuesFunc(a *API) {
839839
return
840840
}
841841

842-
func (s *service) generateConvertCode(name, typ string) {
842+
func (s *service) generateConvertCode(name, cmdName, typ string) {
843843
pn := s.pn
844844

845845
switch typ {
@@ -862,7 +862,12 @@ func (s *service) generateConvertCode(name, typ string) {
862862
pn("for k, vv := range v.(map[string]string) {")
863863
switch name {
864864
case "details":
865-
pn(" u.Set(fmt.Sprintf(\"%s[%%d].%%s\", i, k), vv)", name)
865+
if detailsRequiresKeyValue(cmdName) {
866+
pn(" u.Set(fmt.Sprintf(\"%s[%%d].key\", i), k)", name)
867+
pn(" u.Set(fmt.Sprintf(\"%s[%%d].value\", i), vv)", name)
868+
} else {
869+
pn(" u.Set(fmt.Sprintf(\"%s[%%d].%%s\", i, k), vv)", name)
870+
}
866871
case "serviceproviderlist":
867872
pn(" u.Set(fmt.Sprintf(\"%s[%%d].service\", i), k)", name)
868873
pn(" u.Set(fmt.Sprintf(\"%s[%%d].provider\", i), vv)", name)
@@ -879,6 +884,24 @@ func (s *service) generateConvertCode(name, typ string) {
879884
return
880885
}
881886

887+
func detailsRequiresKeyValue(cmd string) bool {
888+
var requiredCmds = []string{
889+
"addGuestOs",
890+
"updateGuestOs",
891+
"addImageStore",
892+
"createSecondaryStagingStore",
893+
"updateCloudToUseObjectStore",
894+
"addResourceDetail",
895+
"updateZone",
896+
}
897+
for _, req := range requiredCmds {
898+
if req == cmd {
899+
return true
900+
}
901+
}
902+
return false
903+
}
904+
882905
func (s *service) parseParamName(name string) string {
883906
if name != "type" {
884907
return name

0 commit comments

Comments
 (0)