This repository has been archived by the owner on May 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add iam support for gke hub membership (#6389) (#903)
Signed-off-by: Modular Magician <[email protected]>
- Loading branch information
1 parent
aa7cadc
commit ef3d613
Showing
5 changed files
with
324 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
// ---------------------------------------------------------------------------- | ||
// | ||
// *** 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 GKEHubMembershipIAMAssetType string = "gkehub.googleapis.com/Membership" | ||
|
||
func resourceConverterGKEHubMembershipIamPolicy() ResourceConverter { | ||
return ResourceConverter{ | ||
AssetType: GKEHubMembershipIAMAssetType, | ||
Convert: GetGKEHubMembershipIamPolicyCaiObject, | ||
MergeCreateUpdate: MergeGKEHubMembershipIamPolicy, | ||
} | ||
} | ||
|
||
func resourceConverterGKEHubMembershipIamBinding() ResourceConverter { | ||
return ResourceConverter{ | ||
AssetType: GKEHubMembershipIAMAssetType, | ||
Convert: GetGKEHubMembershipIamBindingCaiObject, | ||
FetchFullResource: FetchGKEHubMembershipIamPolicy, | ||
MergeCreateUpdate: MergeGKEHubMembershipIamBinding, | ||
MergeDelete: MergeGKEHubMembershipIamBindingDelete, | ||
} | ||
} | ||
|
||
func resourceConverterGKEHubMembershipIamMember() ResourceConverter { | ||
return ResourceConverter{ | ||
AssetType: GKEHubMembershipIAMAssetType, | ||
Convert: GetGKEHubMembershipIamMemberCaiObject, | ||
FetchFullResource: FetchGKEHubMembershipIamPolicy, | ||
MergeCreateUpdate: MergeGKEHubMembershipIamMember, | ||
MergeDelete: MergeGKEHubMembershipIamMemberDelete, | ||
} | ||
} | ||
|
||
func GetGKEHubMembershipIamPolicyCaiObject(d TerraformResourceData, config *Config) ([]Asset, error) { | ||
return newGKEHubMembershipIamAsset(d, config, expandIamPolicyBindings) | ||
} | ||
|
||
func GetGKEHubMembershipIamBindingCaiObject(d TerraformResourceData, config *Config) ([]Asset, error) { | ||
return newGKEHubMembershipIamAsset(d, config, expandIamRoleBindings) | ||
} | ||
|
||
func GetGKEHubMembershipIamMemberCaiObject(d TerraformResourceData, config *Config) ([]Asset, error) { | ||
return newGKEHubMembershipIamAsset(d, config, expandIamMemberBindings) | ||
} | ||
|
||
func MergeGKEHubMembershipIamPolicy(existing, incoming Asset) Asset { | ||
existing.IAMPolicy = incoming.IAMPolicy | ||
return existing | ||
} | ||
|
||
func MergeGKEHubMembershipIamBinding(existing, incoming Asset) Asset { | ||
return mergeIamAssets(existing, incoming, mergeAuthoritativeBindings) | ||
} | ||
|
||
func MergeGKEHubMembershipIamBindingDelete(existing, incoming Asset) Asset { | ||
return mergeDeleteIamAssets(existing, incoming, mergeDeleteAuthoritativeBindings) | ||
} | ||
|
||
func MergeGKEHubMembershipIamMember(existing, incoming Asset) Asset { | ||
return mergeIamAssets(existing, incoming, mergeAdditiveBindings) | ||
} | ||
|
||
func MergeGKEHubMembershipIamMemberDelete(existing, incoming Asset) Asset { | ||
return mergeDeleteIamAssets(existing, incoming, mergeDeleteAdditiveBindings) | ||
} | ||
|
||
func newGKEHubMembershipIamAsset( | ||
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, "//gkehub.googleapis.com/projects/{{project}}/locations/{{location}}/memberships/{{membership_id}}") | ||
if err != nil { | ||
return []Asset{}, err | ||
} | ||
|
||
return []Asset{{ | ||
Name: name, | ||
Type: GKEHubMembershipIAMAssetType, | ||
IAMPolicy: &IAMPolicy{ | ||
Bindings: bindings, | ||
}, | ||
}}, nil | ||
} | ||
|
||
func FetchGKEHubMembershipIamPolicy(d TerraformResourceData, config *Config) (Asset, error) { | ||
// Check if the identity field returns a value | ||
if _, ok := d.GetOk("membership_id"); !ok { | ||
return Asset{}, ErrEmptyIdentityField | ||
} | ||
|
||
return fetchIamPolicy( | ||
GKEHubMembershipIamUpdaterProducer, | ||
d, | ||
config, | ||
"//gkehub.googleapis.com/projects/{{project}}/locations/{{location}}/memberships/{{membership_id}}", | ||
GKEHubMembershipIAMAssetType, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,198 @@ | ||
// ---------------------------------------------------------------------------- | ||
// | ||
// *** 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 GKEHubMembershipIamSchema = map[string]*schema.Schema{ | ||
"project": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Optional: true, | ||
ForceNew: true, | ||
}, | ||
"membership_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
DiffSuppressFunc: compareSelfLinkOrResourceName, | ||
}, | ||
} | ||
|
||
type GKEHubMembershipIamUpdater struct { | ||
project string | ||
membershipId string | ||
d TerraformResourceData | ||
Config *Config | ||
} | ||
|
||
func GKEHubMembershipIamUpdaterProducer(d TerraformResourceData, config *Config) (ResourceIamUpdater, error) { | ||
values := make(map[string]string) | ||
|
||
project, _ := getProject(d, config) | ||
if project != "" { | ||
if err := d.Set("project", project); err != nil { | ||
return nil, fmt.Errorf("Error setting project: %s", err) | ||
} | ||
} | ||
values["project"] = project | ||
if v, ok := d.GetOk("membership_id"); ok { | ||
values["membership_id"] = v.(string) | ||
} | ||
|
||
// We may have gotten either a long or short name, so attempt to parse long name if possible | ||
m, err := getImportIdQualifiers([]string{"projects/(?P<project>[^/]+)/locations/(?P<location>[^/]+)/memberships/(?P<membership_id>[^/]+)", "(?P<project>[^/]+)/(?P<location>[^/]+)/(?P<membership_id>[^/]+)", "(?P<location>[^/]+)/(?P<membership_id>[^/]+)", "(?P<membership_id>[^/]+)"}, d, config, d.Get("membership_id").(string)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for k, v := range m { | ||
values[k] = v | ||
} | ||
|
||
u := &GKEHubMembershipIamUpdater{ | ||
project: values["project"], | ||
membershipId: values["membership_id"], | ||
d: d, | ||
Config: config, | ||
} | ||
|
||
if err := d.Set("project", u.project); err != nil { | ||
return nil, fmt.Errorf("Error setting project: %s", err) | ||
} | ||
if err := d.Set("membership_id", u.GetResourceId()); err != nil { | ||
return nil, fmt.Errorf("Error setting membership_id: %s", err) | ||
} | ||
|
||
return u, nil | ||
} | ||
|
||
func GKEHubMembershipIdParseFunc(d *schema.ResourceData, config *Config) error { | ||
values := make(map[string]string) | ||
|
||
project, _ := getProject(d, config) | ||
if project != "" { | ||
values["project"] = project | ||
} | ||
|
||
m, err := getImportIdQualifiers([]string{"projects/(?P<project>[^/]+)/locations/(?P<location>[^/]+)/memberships/(?P<membership_id>[^/]+)", "(?P<project>[^/]+)/(?P<location>[^/]+)/(?P<membership_id>[^/]+)", "(?P<location>[^/]+)/(?P<membership_id>[^/]+)", "(?P<membership_id>[^/]+)"}, d, config, d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for k, v := range m { | ||
values[k] = v | ||
} | ||
|
||
u := &GKEHubMembershipIamUpdater{ | ||
project: values["project"], | ||
membershipId: values["membership_id"], | ||
d: d, | ||
Config: config, | ||
} | ||
if err := d.Set("membership_id", u.GetResourceId()); err != nil { | ||
return fmt.Errorf("Error setting membership_id: %s", err) | ||
} | ||
d.SetId(u.GetResourceId()) | ||
return nil | ||
} | ||
|
||
func (u *GKEHubMembershipIamUpdater) GetResourceIamPolicy() (*cloudresourcemanager.Policy, error) { | ||
url, err := u.qualifyMembershipUrl("getIamPolicy") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
project, err := getProject(u.d, u.Config) | ||
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, "GET", project, 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 *GKEHubMembershipIamUpdater) 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.qualifyMembershipUrl("setIamPolicy") | ||
if err != nil { | ||
return err | ||
} | ||
project, err := getProject(u.d, u.Config) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
userAgent, err := generateUserAgentString(u.d, u.Config.userAgent) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = sendRequestWithTimeout(u.Config, "POST", project, 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 *GKEHubMembershipIamUpdater) qualifyMembershipUrl(methodIdentifier string) (string, error) { | ||
urlTemplate := fmt.Sprintf("{{GKEHubBasePath}}%s:%s", fmt.Sprintf("projects/%s/locations/global/memberships/%s", u.project, u.membershipId), methodIdentifier) | ||
url, err := replaceVars(u.d, u.Config, urlTemplate) | ||
if err != nil { | ||
return "", err | ||
} | ||
return url, nil | ||
} | ||
|
||
func (u *GKEHubMembershipIamUpdater) GetResourceId() string { | ||
return fmt.Sprintf("projects/%s/locations/global/memberships/%s", u.project, u.membershipId) | ||
} | ||
|
||
func (u *GKEHubMembershipIamUpdater) GetMutexKey() string { | ||
return fmt.Sprintf("iam-gkehub-membership-%s", u.GetResourceId()) | ||
} | ||
|
||
func (u *GKEHubMembershipIamUpdater) DescribeResource() string { | ||
return fmt.Sprintf("gkehub membership %q", u.GetResourceId()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters