Skip to content
This repository has been archived by the owner on Nov 27, 2024. It is now read-only.

feat(vertexai): Add IAM resources for Vertex AI FeatureStore EntityType (beta) #1149

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
182 changes: 182 additions & 0 deletions converters/google/resources/iam_vertex_ai_featurestore_entitytype.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
// ----------------------------------------------------------------------------
//
// *** AUTO GENERATED CODE *** Type: MMv1 ***
//
// ----------------------------------------------------------------------------
//
// This file is automatically generated by Magic Modules and manual
// changes will be clobbered when the file is regenerated.
//
// Please read more about how to change this file in
// .github/CONTRIBUTING.md.
//
// ----------------------------------------------------------------------------

package google

import (
"fmt"

"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"google.golang.org/api/cloudresourcemanager/v1"
)

var VertexAIFeaturestoreEntitytypeIamSchema = map[string]*schema.Schema{
"featurestore": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"entitytype": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
DiffSuppressFunc: compareSelfLinkOrResourceName,
},
}

type VertexAIFeaturestoreEntitytypeIamUpdater struct {
featurestore string
entitytype string
d TerraformResourceData
Config *Config
}

func VertexAIFeaturestoreEntitytypeIamUpdaterProducer(d TerraformResourceData, config *Config) (ResourceIamUpdater, error) {
values := make(map[string]string)

if v, ok := d.GetOk("featurestore"); ok {
values["featurestore"] = v.(string)
}

if v, ok := d.GetOk("entitytype"); ok {
values["entitytype"] = v.(string)
}

// We may have gotten either a long or short name, so attempt to parse long name if possible
m, err := getImportIdQualifiers([]string{"(?P<featurestore>.+)/entityTypes/(?P<entitytype>[^/]+)", "(?P<entitytype>[^/]+)"}, d, config, d.Get("entitytype").(string))
if err != nil {
return nil, err
}

for k, v := range m {
values[k] = v
}

u := &VertexAIFeaturestoreEntitytypeIamUpdater{
featurestore: values["featurestore"],
entitytype: values["entitytype"],
d: d,
Config: config,
}

if err := d.Set("featurestore", u.featurestore); err != nil {
return nil, fmt.Errorf("Error setting featurestore: %s", err)
}
if err := d.Set("entitytype", u.GetResourceId()); err != nil {
return nil, fmt.Errorf("Error setting entitytype: %s", err)
}

return u, nil
}

func VertexAIFeaturestoreEntitytypeIdParseFunc(d *schema.ResourceData, config *Config) error {
values := make(map[string]string)

m, err := getImportIdQualifiers([]string{"(?P<featurestore>.+)/entityTypes/(?P<entitytype>[^/]+)", "(?P<entitytype>[^/]+)"}, d, config, d.Id())
if err != nil {
return err
}

for k, v := range m {
values[k] = v
}

u := &VertexAIFeaturestoreEntitytypeIamUpdater{
featurestore: values["featurestore"],
entitytype: values["entitytype"],
d: d,
Config: config,
}
if err := d.Set("entitytype", u.GetResourceId()); err != nil {
return fmt.Errorf("Error setting entitytype: %s", err)
}
d.SetId(u.GetResourceId())
return nil
}

func (u *VertexAIFeaturestoreEntitytypeIamUpdater) GetResourceIamPolicy() (*cloudresourcemanager.Policy, error) {
url, err := u.qualifyFeaturestoreEntitytypeUrl("getIamPolicy")
if err != nil {
return nil, err
}

var obj map[string]interface{}

userAgent, err := generateUserAgentString(u.d, u.Config.userAgent)
if err != nil {
return nil, err
}

policy, err := sendRequest(u.Config, "POST", "", url, userAgent, obj)
if err != nil {
return nil, errwrap.Wrapf(fmt.Sprintf("Error retrieving IAM policy for %s: {{err}}", u.DescribeResource()), err)
}

out := &cloudresourcemanager.Policy{}
err = Convert(policy, out)
if err != nil {
return nil, errwrap.Wrapf("Cannot convert a policy to a resource manager policy: {{err}}", err)
}

return out, nil
}

func (u *VertexAIFeaturestoreEntitytypeIamUpdater) SetResourceIamPolicy(policy *cloudresourcemanager.Policy) error {
json, err := ConvertToMap(policy)
if err != nil {
return err
}

obj := make(map[string]interface{})
obj["policy"] = json

url, err := u.qualifyFeaturestoreEntitytypeUrl("setIamPolicy")
if err != nil {
return err
}

userAgent, err := generateUserAgentString(u.d, u.Config.userAgent)
if err != nil {
return err
}

_, err = sendRequestWithTimeout(u.Config, "POST", "", url, userAgent, obj, u.d.Timeout(schema.TimeoutCreate))
if err != nil {
return errwrap.Wrapf(fmt.Sprintf("Error setting IAM policy for %s: {{err}}", u.DescribeResource()), err)
}

return nil
}

func (u *VertexAIFeaturestoreEntitytypeIamUpdater) qualifyFeaturestoreEntitytypeUrl(methodIdentifier string) (string, error) {
urlTemplate := fmt.Sprintf("{{VertexAIBasePath}}%s:%s", fmt.Sprintf("%s/entityTypes/%s", u.featurestore, u.entitytype), methodIdentifier)
url, err := replaceVars(u.d, u.Config, urlTemplate)
if err != nil {
return "", err
}
return url, nil
}

func (u *VertexAIFeaturestoreEntitytypeIamUpdater) GetResourceId() string {
return fmt.Sprintf("%s/entityTypes/%s", u.featurestore, u.entitytype)
}

func (u *VertexAIFeaturestoreEntitytypeIamUpdater) GetMutexKey() string {
return fmt.Sprintf("iam-vertexai-featurestoreentitytype-%s", u.GetResourceId())
}

func (u *VertexAIFeaturestoreEntitytypeIamUpdater) DescribeResource() string {
return fmt.Sprintf("vertexai featurestoreentitytype %q", u.GetResourceId())
}
3 changes: 3 additions & 0 deletions converters/google/resources/resource_converters.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,9 @@ func ResourceConverters() map[string][]ResourceConverter {
"google_vertex_ai_featurestore_iam_policy": {resourceConverterVertexAIFeaturestoreIamPolicy()},
"google_vertex_ai_featurestore_iam_binding": {resourceConverterVertexAIFeaturestoreIamBinding()},
"google_vertex_ai_featurestore_iam_member": {resourceConverterVertexAIFeaturestoreIamMember()},
"google_vertex_ai_featurestore_entitytype_iam_policy": {resourceConverterVertexAIFeaturestoreEntitytypeIamPolicy()},
"google_vertex_ai_featurestore_entitytype_iam_binding": {resourceConverterVertexAIFeaturestoreEntitytypeIamBinding()},
"google_vertex_ai_featurestore_entitytype_iam_member": {resourceConverterVertexAIFeaturestoreEntitytypeIamMember()},
"google_project": {
resourceConverterProject(),
resourceConverterProjectBillingInfo(),
Expand Down
123 changes: 123 additions & 0 deletions converters/google/resources/vertex_ai_featurestore_entitytype_iam.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// ----------------------------------------------------------------------------
//
// *** AUTO GENERATED CODE *** Type: MMv1 ***
//
// ----------------------------------------------------------------------------
//
// This file is automatically generated by Magic Modules and manual
// changes will be clobbered when the file is regenerated.
//
// Please read more about how to change this file in
// .github/CONTRIBUTING.md.
//
// ----------------------------------------------------------------------------

package google

import "fmt"

// Provide a separate asset type constant so we don't have to worry about name conflicts between IAM and non-IAM converter files
const VertexAIFeaturestoreEntitytypeIAMAssetType string = "{{region}}-aiplatform.googleapis.com/FeaturestoreEntitytype"

func resourceConverterVertexAIFeaturestoreEntitytypeIamPolicy() ResourceConverter {
return ResourceConverter{
AssetType: VertexAIFeaturestoreEntitytypeIAMAssetType,
Convert: GetVertexAIFeaturestoreEntitytypeIamPolicyCaiObject,
MergeCreateUpdate: MergeVertexAIFeaturestoreEntitytypeIamPolicy,
}
}

func resourceConverterVertexAIFeaturestoreEntitytypeIamBinding() ResourceConverter {
return ResourceConverter{
AssetType: VertexAIFeaturestoreEntitytypeIAMAssetType,
Convert: GetVertexAIFeaturestoreEntitytypeIamBindingCaiObject,
FetchFullResource: FetchVertexAIFeaturestoreEntitytypeIamPolicy,
MergeCreateUpdate: MergeVertexAIFeaturestoreEntitytypeIamBinding,
MergeDelete: MergeVertexAIFeaturestoreEntitytypeIamBindingDelete,
}
}

func resourceConverterVertexAIFeaturestoreEntitytypeIamMember() ResourceConverter {
return ResourceConverter{
AssetType: VertexAIFeaturestoreEntitytypeIAMAssetType,
Convert: GetVertexAIFeaturestoreEntitytypeIamMemberCaiObject,
FetchFullResource: FetchVertexAIFeaturestoreEntitytypeIamPolicy,
MergeCreateUpdate: MergeVertexAIFeaturestoreEntitytypeIamMember,
MergeDelete: MergeVertexAIFeaturestoreEntitytypeIamMemberDelete,
}
}

func GetVertexAIFeaturestoreEntitytypeIamPolicyCaiObject(d TerraformResourceData, config *Config) ([]Asset, error) {
return newVertexAIFeaturestoreEntitytypeIamAsset(d, config, expandIamPolicyBindings)
}

func GetVertexAIFeaturestoreEntitytypeIamBindingCaiObject(d TerraformResourceData, config *Config) ([]Asset, error) {
return newVertexAIFeaturestoreEntitytypeIamAsset(d, config, expandIamRoleBindings)
}

func GetVertexAIFeaturestoreEntitytypeIamMemberCaiObject(d TerraformResourceData, config *Config) ([]Asset, error) {
return newVertexAIFeaturestoreEntitytypeIamAsset(d, config, expandIamMemberBindings)
}

func MergeVertexAIFeaturestoreEntitytypeIamPolicy(existing, incoming Asset) Asset {
existing.IAMPolicy = incoming.IAMPolicy
return existing
}

func MergeVertexAIFeaturestoreEntitytypeIamBinding(existing, incoming Asset) Asset {
return mergeIamAssets(existing, incoming, mergeAuthoritativeBindings)
}

func MergeVertexAIFeaturestoreEntitytypeIamBindingDelete(existing, incoming Asset) Asset {
return mergeDeleteIamAssets(existing, incoming, mergeDeleteAuthoritativeBindings)
}

func MergeVertexAIFeaturestoreEntitytypeIamMember(existing, incoming Asset) Asset {
return mergeIamAssets(existing, incoming, mergeAdditiveBindings)
}

func MergeVertexAIFeaturestoreEntitytypeIamMemberDelete(existing, incoming Asset) Asset {
return mergeDeleteIamAssets(existing, incoming, mergeDeleteAdditiveBindings)
}

func newVertexAIFeaturestoreEntitytypeIamAsset(
d TerraformResourceData,
config *Config,
expandBindings func(d TerraformResourceData) ([]IAMBinding, error),
) ([]Asset, error) {
bindings, err := expandBindings(d)
if err != nil {
return []Asset{}, fmt.Errorf("expanding bindings: %v", err)
}

name, err := assetName(d, config, "//{{region}}-aiplatform.googleapis.com/{{featurestore}}/entityTypes/{{entitytype}}")
if err != nil {
return []Asset{}, err
}

return []Asset{{
Name: name,
Type: VertexAIFeaturestoreEntitytypeIAMAssetType,
IAMPolicy: &IAMPolicy{
Bindings: bindings,
},
}}, nil
}

func FetchVertexAIFeaturestoreEntitytypeIamPolicy(d TerraformResourceData, config *Config) (Asset, error) {
// Check if the identity field returns a value
if _, ok := d.GetOk("featurestore"); !ok {
return Asset{}, ErrEmptyIdentityField
}
if _, ok := d.GetOk("entitytype"); !ok {
return Asset{}, ErrEmptyIdentityField
}

return fetchIamPolicy(
VertexAIFeaturestoreEntitytypeIamUpdaterProducer,
d,
config,
"//{{region}}-aiplatform.googleapis.com/{{featurestore}}/entityTypes/{{entitytype}}",
VertexAIFeaturestoreEntitytypeIAMAssetType,
)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ require (
github.com/hashicorp/go-cleanhttp v0.5.2
github.com/hashicorp/terraform-json v0.14.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.18.0
github.com/hashicorp/terraform-provider-google v1.20.1-0.20221128172449-cb42c191b138
github.com/hashicorp/terraform-provider-google v1.20.1-0.20221128184404-d02c5c7f84ca
github.com/mitchellh/go-homedir v1.1.0
github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.8.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -658,8 +658,8 @@ github.com/hashicorp/terraform-plugin-log v0.4.1 h1:xpbmVhvuU3mgHzLetOmx9pkOL2rm
github.com/hashicorp/terraform-plugin-log v0.4.1/go.mod h1:p4R1jWBXRTvL4odmEkFfDdhUjHf9zcs/BCoNHAc7IK4=
github.com/hashicorp/terraform-plugin-sdk/v2 v2.18.0 h1:/cdI5di5XA+N80gXzXF4YcHq36DprBskubk6Z8i26ZQ=
github.com/hashicorp/terraform-plugin-sdk/v2 v2.18.0/go.mod h1:L3SHkD/Q8zPVgXviQmpVwy9nKwpXXZscVIpVEnQ/T50=
github.com/hashicorp/terraform-provider-google v1.20.1-0.20221128172449-cb42c191b138 h1:9sTbaaOhPi9Cq2Wn7UjVsNVtiVLI4tPEQohm3BBuQWg=
github.com/hashicorp/terraform-provider-google v1.20.1-0.20221128172449-cb42c191b138/go.mod h1:J1q/lz9dSnciHwY+rjQRs0r+M3pe0+0G887jFq8COvM=
github.com/hashicorp/terraform-provider-google v1.20.1-0.20221128184404-d02c5c7f84ca h1:US7vpfpgtYwTvpD5dSSHos47kAQwJqKZ6Pp/UNqPKPk=
github.com/hashicorp/terraform-provider-google v1.20.1-0.20221128184404-d02c5c7f84ca/go.mod h1:J1q/lz9dSnciHwY+rjQRs0r+M3pe0+0G887jFq8COvM=
github.com/hashicorp/terraform-registry-address v0.0.0-20220623143253-7d51757b572c h1:D8aRO6+mTqHfLsK/BC3j5OAoogv1WLRWzY1AaTo3rBg=
github.com/hashicorp/terraform-registry-address v0.0.0-20220623143253-7d51757b572c/go.mod h1:Wn3Na71knbXc1G8Lh+yu/dQWWJeFQEpDeJMtWMtlmNI=
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734 h1:HKLsbzeOsfXmKNpr3GiT18XAblV0BjCbzL8KQAMZGa0=
Expand Down