Handler hostcatalogs sdk (#103)

* Created host catalog service.

* Fixing uri generation for create.

* Use newly submitted static host catalog.

* Copying over tests from projects.

* Fix UpdateHostCatalogRequest merging proto.

* Properly use static host catalog prefixes in tests.

* Correct test to verify the expected uri and id prefix is set.

* Add test for unsetting descriptions.

* Correct bad spelling in comment.

* Tweaking variable names.

* Adding Stringer to the catalog type and setting the type on the response.

* Setting up the catalogType to contain more info regarding its type.

* Validate the type field in requests.

* Remove unused method.

* Comment describing invariants around types.

* Wiring in the repo into the static catalog handler.

* feature:  Add CRUD operations for host catalogs to the go api library.

* Updated comments to the exported methods.

* fix: return the result of handlers.NotFoundError instead of just calling it.

* Fixing typos referencing just hosts instead of host catlogs.

* Update NewService returns an error and a non pointer type.

* Pull "org_id" and "project_id" into constants and the validation of them into the ValidateAncestors.

* Check errors for all Register*HandlerServer calls.

* fix: Remove left over reference that could cause a panic.

* fix: Changing "x := []string{}" to "var x []string"

* fix: Log errors in test cleanup.

* Changing the code to use google.golang.org/protobuf instead of the older github based protobuf library.

* Changing all t.Fatal checks to require.NoError calls.

* Changing unrecognized prefixes to Invalid Argument instead of Not Found.

* fix: toDbUpdateMask now returns error if it was unable to find any valid paths.

* Define CreatedTime, UpdatedTime, and Type at initialization time in toProto.

* Adding sdk test for unsetting a value.

* Require that project is created correctly before testing the catalog.

* Remove unused host catalog from test.

* Change a t.Fatal to a require.NoError that I missed earlier.

* Change shutdown error logging to t.Errorfs.

* Shorten validateX function names.

* Inline API responses.

* Add todo to make a new repo per request.

* Inline regex check for invalid id.

* Change stored repos into repo creating functions which satisfies ICU-223.

* Wrap proto comments to be under 80 characters long.

* Remove the exception on the read only fields that can be set in an Update request.

* Removing out of date comment.

* Use iam.TestScopes instead of making the project myself.

* Renaming files to be consistant with go test standards and resource naming standards.

* Remove TODO related to a task completed in an earlier commit.

* Fix the previous commit which removed the wrong TODO.
This commit is contained in:
Todd Knight authored and GitHub committed 2020-06-05 13:02:58 -07:00
1 parent 7f6e78e66e
commit d374ac6d4c
11 files changed
+413 -111

No files matched your search

+24 -19
View File
@@ -18,21 +18,26 @@ type createInfo struct {
var createFuncs = map[string][]*createInfo{
"hosts": {
{
"HostCatalog",
"Host",
`fmt.Sprintf("host-catalogs/%s/hosts", s.Id)`,
baseType: "HostCatalog",
targetType: "Host",
path: `fmt.Sprintf("host-catalogs/%s/hosts", s.Id)`,
},
{
"HostCatalog",
"HostSet",
`fmt.Sprintf("host-catalogs/%s/host-sets", s.Id)`,
baseType: "HostCatalog",
targetType: "HostSet",
path: `fmt.Sprintf("host-catalogs/%s/host-sets", s.Id)`,
},
},
"scopes": {
{
"Organization",
"Project",
`"projects"`,
baseType: "Organization",
targetType: "Project",
path: `"projects"`,
},
{
baseType: "Project",
targetType: "hosts.HostCatalog",
path: `"host-catalogs"`,
},
},
}
@@ -46,15 +51,15 @@ package %s
`, outPkg)))
for _, createInfo := range funcs {
createFuncTemplate.Execute(outBuf, struct {
BaseType string
TargetType string
LowerTargetType string
Path string
BaseType string
TargetType string
TargetName string
Path string
}{
BaseType: createInfo.baseType,
TargetType: createInfo.targetType,
LowerTargetType: strings.ToLower(createInfo.targetType),
Path: createInfo.path,
BaseType: createInfo.baseType,
TargetType: createInfo.targetType,
TargetName: strings.Split(createInfo.targetType, ".")[strings.Count(createInfo.targetType, ".")],
Path: createInfo.path,
})
}
if err := ioutil.WriteFile(outFile, outBuf.Bytes(), 0644); err != nil {
@@ -66,7 +71,7 @@ package %s
var createFuncTemplate = template.Must(template.New("").Parse(
`
func (s {{ .BaseType }}) Create{{ .TargetType }}(ctx context.Context, {{ .LowerTargetType }} *{{ .TargetType }}) (*{{ .TargetType }}, *api.Error, error) {
func (s {{ .BaseType }}) Create{{ .TargetName }}(ctx context.Context, r *{{ .TargetType }}) (*{{ .TargetType }}, *api.Error, error) {
if s.Client == nil {
return nil, nil, fmt.Errorf("nil client in Create{{ .TargetType }} request")
}
@@ -90,7 +95,7 @@ func (s {{ .BaseType }}) Create{{ .TargetType }}(ctx context.Context, {{ .LowerT
{{ end }}
}
req, err := s.Client.NewRequest(ctx, "POST", {{ .Path }}, {{ .LowerTargetType }})
req, err := s.Client.NewRequest(ctx, "POST", {{ .Path }}, r)
if err != nil {
return nil, nil, fmt.Errorf("error creating Create{{ .TargetType }} request: %w", err)
}
+24 -19
View File
@@ -20,7 +20,12 @@ var deleteFuncs = map[string][]*deleteInfo{
{
"Organization",
"Project",
"projects/%s",
"projects",
},
{
"Project",
"hosts.HostCatalog",
"host-catalogs",
},
},
}
@@ -34,15 +39,15 @@ package %s
`, outPkg)))
for _, deleteInfo := range funcs {
deleteFuncTemplate.Execute(outBuf, struct {
BaseType string
TargetType string
LowerTargetType string
Path string
BaseType string
TargetType string
TargetName string
Path string
}{
BaseType: deleteInfo.baseType,
TargetType: deleteInfo.targetType,
LowerTargetType: strings.ToLower(deleteInfo.targetType),
Path: deleteInfo.path,
BaseType: deleteInfo.baseType,
TargetType: deleteInfo.targetType,
TargetName: strings.Split(deleteInfo.targetType, ".")[strings.Count(deleteInfo.targetType, ".")],
Path: deleteInfo.path,
})
}
if err := ioutil.WriteFile(outFile, outBuf.Bytes(), 0644); err != nil {
@@ -54,10 +59,10 @@ package %s
var deleteFuncTemplate = template.Must(template.New("").Parse(
`
// Delete{{ .TargetType }} returns true iff the {{ .LowerTargetType }} existed when the delete attempt was made.
func (s {{ .BaseType }}) Delete{{ .TargetType }}(ctx context.Context, {{ .LowerTargetType }} *{{ .TargetType }}) (bool, *api.Error, error) {
// Delete{{ .TargetName }} returns true iff the {{ .TargetType }} existed when the delete attempt was made.
func (s {{ .BaseType }}) Delete{{ .TargetName }}(ctx context.Context, r *{{ .TargetType }}) (bool, *api.Error, error) {
if s.Client == nil {
return false, nil, fmt.Errorf("nil client in Delete{{ .TargetType }} request")
return false, nil, fmt.Errorf("nil client in Delete{{ .TargetName }} request")
}
if s.Id == "" {
{{ if (eq .BaseType "Organization") }}
@@ -67,7 +72,7 @@ func (s {{ .BaseType }}) Delete{{ .TargetType }}(ctx context.Context, {{ .LowerT
// Assume the client has been configured with project already and move
// on
{{ else }}
return nil, nil, fmt.Errorf("missing {{ .BaseType }} ID in Delete{{ .TargetType }} request")
return nil, nil, fmt.Errorf("missing {{ .BaseType }} ID in Delete{{ .TargetName }} request")
{{ end }}
} else {
// If it's explicitly set here, override anything that might be in the
@@ -78,18 +83,18 @@ func (s {{ .BaseType }}) Delete{{ .TargetType }}(ctx context.Context, {{ .LowerT
ctx = context.WithValue(ctx, "project", s.Id)
{{ end }}
}
if {{ .LowerTargetType }}.Id == "" {
return false, nil, fmt.Errorf("empty {{ .LowerTargetType }} ID field in Delete{{ .TargetType }} request")
if r.Id == "" {
return false, nil, fmt.Errorf("empty {{ .TargetType }} ID field in Delete{{ .TargetName }} request")
}
req, err := s.Client.NewRequest(ctx, "DELETE", fmt.Sprintf("{{ .Path }}", {{ .LowerTargetType }}.Id), nil)
req, err := s.Client.NewRequest(ctx, "DELETE", fmt.Sprintf("%s/%s", "{{ .Path }}", r.Id), nil)
if err != nil {
return false, nil, fmt.Errorf("error creating Delete{{ .TargetType }} request: %w", err)
return false, nil, fmt.Errorf("error creating Delete{{ .TargetName }} request: %w", err)
}
resp, err := s.Client.Do(req)
if err != nil {
return false, nil, fmt.Errorf("error performing client request during Delete{{ .TargetType }} call: %w", err)
return false, nil, fmt.Errorf("error performing client request during Delete{{ .TargetName }} call: %w", err)
}
type deleteResponse struct {
@@ -99,7 +104,7 @@ func (s {{ .BaseType }}) Delete{{ .TargetType }}(ctx context.Context, {{ .LowerT
apiErr, err := resp.Decode(target)
if err != nil {
return false, nil, fmt.Errorf("error decoding Delete{{ .TargetType }} repsonse: %w", err)
return false, nil, fmt.Errorf("error decoding Delete{{ .TargetName }} repsonse: %w", err)
}
return target.Existed, apiErr, nil
+24 -23
View File
@@ -12,17 +12,20 @@ import (
type readInfo struct {
baseType string
targetType string
verb string
path string
}
var readFuncs = map[string][]*readInfo{
"scopes": {
{
"Organization",
"Project",
"GET",
"projects/%s",
baseType: "Organization",
targetType: "Project",
path: "projects",
},
{
baseType: "Project",
targetType: "hosts.HostCatalog",
path: "host-catalogs",
},
},
}
@@ -36,17 +39,15 @@ package %s
`, outPkg)))
for _, readInfo := range funcs {
readFuncTemplate.Execute(outBuf, struct {
BaseType string
TargetType string
LowerTargetType string
Verb string
Path string
BaseType string
TargetType string
TargetName string
Path string
}{
BaseType: readInfo.baseType,
TargetType: readInfo.targetType,
LowerTargetType: strings.ToLower(readInfo.targetType),
Verb: readInfo.verb,
Path: readInfo.path,
BaseType: readInfo.baseType,
TargetType: readInfo.targetType,
TargetName: strings.Split(readInfo.targetType, ".")[strings.Count(readInfo.targetType, ".")],
Path: readInfo.path,
})
}
if err := ioutil.WriteFile(outFile, outBuf.Bytes(), 0644); err != nil {
@@ -58,9 +59,9 @@ package %s
var readFuncTemplate = template.Must(template.New("").Parse(
`
func (s {{ .BaseType }}) Read{{ .TargetType }}(ctx context.Context, {{ .LowerTargetType }} *{{ .TargetType }}) (*{{ .TargetType }}, *api.Error, error) {
func (s {{ .BaseType }}) Read{{ .TargetName }}(ctx context.Context, r *{{ .TargetType }}) (*{{ .TargetType }}, *api.Error, error) {
if s.Client == nil {
return nil, nil, fmt.Errorf("nil client in Read{{ .TargetType }} request")
return nil, nil, fmt.Errorf("nil client in Read{{ .TargetName }} request")
}
if s.Id == "" {
{{ if (eq .BaseType "Organization") }}
@@ -81,24 +82,24 @@ func (s {{ .BaseType }}) Read{{ .TargetType }}(ctx context.Context, {{ .LowerTar
ctx = context.WithValue(ctx, "project", s.Id)
{{ end }}
}
if {{ .LowerTargetType }}.Id == "" {
return nil, nil, fmt.Errorf("empty {{ .LowerTargetType }} ID field in Read{{ .TargetType }} request")
if r.Id == "" {
return nil, nil, fmt.Errorf("empty {{ .TargetType }} ID field in Read{{ .TargetName }} request")
}
req, err := s.Client.NewRequest(ctx, "{{ .Verb }}", fmt.Sprintf("{{ .Path }}", {{ .LowerTargetType }}.Id), {{ .LowerTargetType }})
req, err := s.Client.NewRequest(ctx, "GET", fmt.Sprintf("%s/%s", "{{ .Path }}", r.Id), r)
if err != nil {
return nil, nil, fmt.Errorf("error creating Read{{ .TargetType }} request: %w", err)
return nil, nil, fmt.Errorf("error creating Read{{ .TargetName }} request: %w", err)
}
resp, err := s.Client.Do(req)
if err != nil {
return nil, nil, fmt.Errorf("error performing client request during Read{{ .TargetType }} call: %w", err)
return nil, nil, fmt.Errorf("error performing client request during Read{{ .TargetName }} call: %w", err)
}
target := new({{ .TargetType }})
apiErr, err := resp.Decode(target)
if err != nil {
return nil, nil, fmt.Errorf("error decoding Read{{ .TargetType }} repsonse: %w", err)
return nil, nil, fmt.Errorf("error decoding Read{{ .TargetName }} repsonse: %w", err)
}
{{ if (eq .TargetType "Organization") }}
+25 -20
View File
@@ -18,9 +18,14 @@ type updateInfo struct {
var updateFuncs = map[string][]*updateInfo{
"scopes": {
{
"Organization",
"Project",
"projects/%s",
baseType: "Organization",
targetType: "Project",
path: "projects",
},
{
baseType: "Project",
targetType: "hosts.HostCatalog",
path: "host-catalogs",
},
},
}
@@ -34,15 +39,15 @@ package %s
`, outPkg)))
for _, updateInfo := range funcs {
updateFuncTemplate.Execute(outBuf, struct {
BaseType string
TargetType string
LowerTargetType string
Path string
BaseType string
TargetType string
TargetName string
Path string
}{
BaseType: updateInfo.baseType,
TargetType: updateInfo.targetType,
LowerTargetType: strings.ToLower(updateInfo.targetType),
Path: updateInfo.path,
BaseType: updateInfo.baseType,
TargetType: updateInfo.targetType,
TargetName: strings.Split(updateInfo.targetType, ".")[strings.Count(updateInfo.targetType, ".")],
Path: updateInfo.path,
})
}
if err := ioutil.WriteFile(outFile, outBuf.Bytes(), 0644); err != nil {
@@ -54,9 +59,9 @@ package %s
var updateFuncTemplate = template.Must(template.New("").Parse(
`
func (s {{ .BaseType }}) Update{{ .TargetType }}(ctx context.Context, {{ .LowerTargetType }} *{{ .TargetType }}) (*{{ .TargetType }}, *api.Error, error) {
func (s {{ .BaseType }}) Update{{ .TargetName }}(ctx context.Context, r *{{ .TargetType }}) (*{{ .TargetType }}, *api.Error, error) {
if s.Client == nil {
return nil, nil, fmt.Errorf("nil client in Create{{ .TargetType }} request")
return nil, nil, fmt.Errorf("nil client in Create{{ .TargetName }} request")
}
if s.Id == "" {
{{ if (eq .BaseType "Organization") }}
@@ -66,7 +71,7 @@ func (s {{ .BaseType }}) Update{{ .TargetType }}(ctx context.Context, {{ .LowerT
// Assume the client has been configured with project already and move
// on
{{ else }}
return nil, nil, fmt.Errorf("missing {{ .BaseType}} ID in Create{{ .TargetType }} request")
return nil, nil, fmt.Errorf("missing {{ .BaseType}} ID in Create{{ .TargetName }} request")
{{ end }}
} else {
// If it's explicitly set here, override anything that might be in the
@@ -78,23 +83,23 @@ func (s {{ .BaseType }}) Update{{ .TargetType }}(ctx context.Context, {{ .LowerT
{{ end }}
}
id := {{ .LowerTargetType }}.Id
{{ .LowerTargetType }}.Id = ""
id := r.Id
r.Id = ""
req, err := s.Client.NewRequest(ctx, "PATCH", fmt.Sprintf("{{ .Path }}", id), {{ .LowerTargetType }})
req, err := s.Client.NewRequest(ctx, "PATCH", fmt.Sprintf("%s/%s", "{{ .Path }}", id), r)
if err != nil {
return nil, nil, fmt.Errorf("error creating Create{{ .TargetType }} request: %w", err)
return nil, nil, fmt.Errorf("error creating Create{{ .TargetName }} request: %w", err)
}
resp, err := s.Client.Do(req)
if err != nil {
return nil, nil, fmt.Errorf("error performing client request during Update{{ .TargetType }} call: %w", err)
return nil, nil, fmt.Errorf("error performing client request during Update{{ .TargetName }} call: %w", err)
}
target := new({{ .TargetType }})
apiErr, err := resp.Decode(target)
if err != nil {
return nil, nil, fmt.Errorf("error decoding Update{{ .TargetType }} repsonse: %w", err)
return nil, nil, fmt.Errorf("error decoding Update{{ .TargetName }} repsonse: %w", err)
}
{{ if (eq .TargetType "Organization") }}