Skip to content

Commit

Permalink
Force parameters to be required to maintain backward compatability
Browse files Browse the repository at this point in the history
  • Loading branch information
vishesh92 committed Mar 5, 2024
1 parent afbda39 commit 86ce941
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ SHELL = /usr/bin/env bash -o pipefail
all: code mocks test

code:
go run generate/generate.go generate/layout.go --api=generate/listApis.json
go run generate/generate.go generate/layout.go generate/requiredParams.go --api=generate/listApis.json

FILES=$(shell for file in `pwd`/cloudstack/*Service.go ;do basename $$file .go ; done)
mocks:
Expand Down
32 changes: 21 additions & 11 deletions generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -1128,7 +1128,7 @@ func (s *service) generateAPITest(a *API) {
pn(" }")
p(" p := client.%s.New%s(", strings.TrimSuffix(s.name, "Service"), tn)
for _, ap := range a.Params {
if ap.Required {
if ap.Required || isRequiredParam(a, ap) {
rp = append(rp, ap)
p("%s, ", getDefaultValueForType(a.Name, ap.Name, ap.Type))
}
Expand Down Expand Up @@ -1196,8 +1196,7 @@ func (s *service) generateInterfaceType() {
// NewParam funcs
p("New%s(", tn)
for _, ap := range api.Params {
if ap.Required {
// rp = append(rp, ap)
if ap.Required || isRequiredParam(api, ap) {
p("%s %s, ", s.parseParamName(ap.Name), mapType(api.Name, ap.Name, ap.Type))
}
}
Expand All @@ -1219,7 +1218,7 @@ func (s *service) generateInterfaceType() {
// Generate the function signature
p("Get%sID(%s string, ", parseSingular(ln), v)
for _, ap := range api.Params {
if ap.Required {
if ap.Required || isRequiredParam(api, ap) {
p("%s %s, ", s.parseParamName(ap.Name), mapType(api.Name, ap.Name, ap.Type))
}
}
Expand All @@ -1234,7 +1233,7 @@ func (s *service) generateInterfaceType() {
if hasIDParamField(api.Name, api.Params) {
p("Get%sByName(name string, ", parseSingular(ln))
for _, ap := range api.Params {
if ap.Required {
if ap.Required || isRequiredParam(api, ap) {
p("%s %s, ", s.parseParamName(ap.Name), mapType(api.Name, ap.Name, ap.Type))
}
}
Expand Down Expand Up @@ -1420,7 +1419,7 @@ func (s *service) generateNewParamTypeFunc(a *API) {
pn("// as then you are sure you have configured all required params")
p("func (s *%s) New%s(", s.name, tn)
for _, ap := range a.Params {
if ap.Required {
if ap.Required || isRequiredParam(a, ap) {
rp = append(rp, ap)
p("%s %s, ", s.parseParamName(ap.Name), mapType(a.Name, ap.Name, ap.Type))
}
Expand Down Expand Up @@ -1458,7 +1457,7 @@ func (s *service) generateHelperFuncs(a *API) {
pn("// This is a courtesy helper function, which in some cases may not work as expected!")
p("func (s *%s) Get%sID(%s string, ", s.name, parseSingular(ln), v)
for _, ap := range a.Params {
if ap.Required {
if ap.Required || isRequiredParam(a, ap) {
p("%s %s, ", s.parseParamName(ap.Name), mapType(a.Name, ap.Name, ap.Type))
}
}
Expand All @@ -1476,7 +1475,7 @@ func (s *service) generateHelperFuncs(a *API) {
pn("")
pn(" p.p[\"%s\"] = %s", v, v)
for _, ap := range a.Params {
if ap.Required {
if ap.Required || isRequiredParam(a, ap) {
pn(" p.p[\"%s\"] = %s", ap.Name, s.parseParamName(ap.Name))
}
}
Expand Down Expand Up @@ -1528,7 +1527,7 @@ func (s *service) generateHelperFuncs(a *API) {
pn("// This is a courtesy helper function, which in some cases may not work as expected!")
p("func (s *%s) Get%sByName(name string, ", s.name, parseSingular(ln))
for _, ap := range a.Params {
if ap.Required {
if ap.Required || isRequiredParam(a, ap) {
p("%s %s, ", s.parseParamName(ap.Name), mapType(a.Name, ap.Name, ap.Type))
}
}
Expand All @@ -1543,7 +1542,7 @@ func (s *service) generateHelperFuncs(a *API) {
// Generate the function body
p(" id, count, err := s.Get%sID(name, ", parseSingular(ln))
for _, ap := range a.Params {
if ap.Required {
if ap.Required || isRequiredParam(a, ap) {
p("%s, ", s.parseParamName(ap.Name))
}
}
Expand All @@ -1560,7 +1559,7 @@ func (s *service) generateHelperFuncs(a *API) {
pn("")
p(" r, count, err := s.Get%sByID(id, ", parseSingular(ln))
for _, ap := range a.Params {
if ap.Required {
if ap.Required || isRequiredParam(a, ap) {
p("%s, ", s.parseParamName(ap.Name))
}
}
Expand Down Expand Up @@ -2116,6 +2115,17 @@ func testDir() (string, error) {

}

func isRequiredParam(api *API, apiParam *APIParam) bool {
if params, ok := requiredParams[api.Name]; ok {
for _, param := range params {
if param == apiParam.Name {
return true
}
}
}
return false
}

func mapType(aName string, pName string, pType string) string {
if _, ok := longToStringConvertedParams[pName]; ok {
pType = "UUID"
Expand Down
54 changes: 54 additions & 0 deletions generate/requiredParams.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//

package main

// This map contains the API commands and the parameters which needs to be made
// made required to ensure backward compatibility with the older versions of
// the CloudStack API.

var requiredParams = map[string][]string{
"createNetworkOffering": {
"displaytext",
},
"createDiskOffering": {
"displaytext",
},
"createServiceOffering": {
"displaytext",
},
"createVPCOffering": {
"displaytext",
},
"registerIso": {
"displaytext",
},
"createProject": {
"displaytext",
},
"createTemplate": {
"displaytext",
},
"registerTemplate": {
"displaytext",
},
"createVPC": {
"displaytext",
},
}

0 comments on commit 86ce941

Please sign in to comment.