Skip to content

Commit

Permalink
added project service to validator (#4127) (#571)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Oct 29, 2020
1 parent 81fbda4 commit fec5a8f
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions google/project_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package google

import (
"fmt"
"reflect"
)

func GetServiceUsageCaiObject(d TerraformResourceData, config *Config) (Asset, error) {
name, err := assetName(d, config, "//serviceusage.googleapis.com/projects/{{project}}/services/{{service}}")
if err != nil {
return Asset{}, err
}
if obj, err := GetServiceUsageApiObject(d, config); err == nil {
return Asset{
Name: name,
Type: "serviceusage.googleapis.com/Service",
Resource: &AssetResource{
Version: "v1",
DiscoveryDocumentURI: "https://www.googleapis.com/discovery/v1/apis/serviceusage/v1/rest",
DiscoveryName: "Service",
Data: obj,
},
}, nil
}
return Asset{}, err
}

func GetServiceUsageApiObject(d TerraformResourceData, config *Config) (map[string]interface{}, error) {
obj := make(map[string]interface{})
parentProjectProp, err := expandServiceUsageParentProject(d.Get("project"), d, config)
if err != nil {
return nil, err
}
obj["parent"] = parentProjectProp

serviceNameProp, err := expandServiceUsageServiceName(d.Get("service"), d, config)
if err != nil {
return nil, err
} else if v, ok := d.GetOkExists("service"); !isEmptyValue(reflect.ValueOf(serviceNameProp)) && (ok || !reflect.DeepEqual(v, serviceNameProp)) {
obj["name"] = serviceNameProp
}

obj["state"] = "ENABLED"

return obj, nil
}

func expandServiceUsageParentProject(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
if v == nil || v.(string) == "" {
// It does not try to construct anything from empty.
return "", nil
}
// Ideally we should use project_number, but since that is generated server-side,
// we substitute project_id.
return fmt.Sprintf("projects/%s", v.(string)), nil
}

func expandServiceUsageServiceName(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

0 comments on commit fec5a8f

Please sign in to comment.