blob: bc732bb8e3e910bba73f5ca14de658c04786a9e2 [file] [log] [blame]
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
// ----------------------------------------------------------------------------
//
// *** 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 gkehub2
import (
"fmt"
"log"
"reflect"
"strings"
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgresource"
transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport"
)
func ResourceGKEHub2MembershipBinding() *schema.Resource {
return &schema.Resource{
Create: resourceGKEHub2MembershipBindingCreate,
Read: resourceGKEHub2MembershipBindingRead,
Update: resourceGKEHub2MembershipBindingUpdate,
Delete: resourceGKEHub2MembershipBindingDelete,
Importer: &schema.ResourceImporter{
State: resourceGKEHub2MembershipBindingImport,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(20 * time.Minute),
Update: schema.DefaultTimeout(20 * time.Minute),
Delete: schema.DefaultTimeout(20 * time.Minute),
},
CustomizeDiff: customdiff.All(
tpgresource.SetLabelsDiff,
tpgresource.DefaultProviderProject,
),
Schema: map[string]*schema.Schema{
"location": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `Location of the membership`,
},
"membership_binding_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `The client-provided identifier of the membership binding.`,
},
"membership_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `Id of the membership`,
},
"scope": {
Type: schema.TypeString,
Required: true,
DiffSuppressFunc: tpgresource.ProjectNumberDiffSuppress,
Description: `A Workspace resource name in the format
'projects/*/locations/*/scopes/*'.`,
},
"labels": {
Type: schema.TypeMap,
Optional: true,
Description: `Labels for this Membership binding.
**Note**: This field is non-authoritative, and will only manage the labels present in your configuration.
Please refer to the field 'effective_labels' for all of the labels present on the resource.`,
Elem: &schema.Schema{Type: schema.TypeString},
},
"create_time": {
Type: schema.TypeString,
Computed: true,
Description: `Time the MembershipBinding was created in UTC.`,
},
"delete_time": {
Type: schema.TypeString,
Computed: true,
Description: `Time the MembershipBinding was deleted in UTC.`,
},
"effective_labels": {
Type: schema.TypeMap,
Computed: true,
Description: `All of labels (key/value pairs) present on the resource in GCP, including the labels configured through Terraform, other clients and services.`,
Elem: &schema.Schema{Type: schema.TypeString},
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: `The resource name for the membershipbinding itself`,
},
"state": {
Type: schema.TypeList,
Computed: true,
Description: `State of the membership binding resource.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"code": {
Type: schema.TypeString,
Computed: true,
Description: `Code describes the state of a MembershipBinding resource.`,
},
},
},
},
"terraform_labels": {
Type: schema.TypeMap,
Computed: true,
Description: `The combination of labels configured directly on the resource
and default labels configured on the provider.`,
Elem: &schema.Schema{Type: schema.TypeString},
},
"uid": {
Type: schema.TypeString,
Computed: true,
Description: `Google-generated UUID for this resource.`,
},
"update_time": {
Type: schema.TypeString,
Computed: true,
Description: `Time the MembershipBinding was updated in UTC.`,
},
"project": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
},
UseJSONNumber: true,
}
}
func resourceGKEHub2MembershipBindingCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*transport_tpg.Config)
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
if err != nil {
return err
}
obj := make(map[string]interface{})
scopeProp, err := expandGKEHub2MembershipBindingScope(d.Get("scope"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("scope"); !tpgresource.IsEmptyValue(reflect.ValueOf(scopeProp)) && (ok || !reflect.DeepEqual(v, scopeProp)) {
obj["scope"] = scopeProp
}
labelsProp, err := expandGKEHub2MembershipBindingEffectiveLabels(d.Get("effective_labels"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("effective_labels"); !tpgresource.IsEmptyValue(reflect.ValueOf(labelsProp)) && (ok || !reflect.DeepEqual(v, labelsProp)) {
obj["labels"] = labelsProp
}
url, err := tpgresource.ReplaceVars(d, config, "{{GKEHub2BasePath}}projects/{{project}}/locations/{{location}}/memberships/{{membership_id}}/bindings/?membership_binding_id={{membership_binding_id}}")
if err != nil {
return err
}
log.Printf("[DEBUG] Creating new MembershipBinding: %#v", obj)
billingProject := ""
project, err := tpgresource.GetProject(d, config)
if err != nil {
return fmt.Errorf("Error fetching project for MembershipBinding: %s", err)
}
billingProject = project
// err == nil indicates that the billing_project value was found
if bp, err := tpgresource.GetBillingProject(d, config); err == nil {
billingProject = bp
}
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: config,
Method: "POST",
Project: billingProject,
RawURL: url,
UserAgent: userAgent,
Body: obj,
Timeout: d.Timeout(schema.TimeoutCreate),
})
if err != nil {
return fmt.Errorf("Error creating MembershipBinding: %s", err)
}
// Store the ID now
id, err := tpgresource.ReplaceVars(d, config, "projects/{{project}}/locations/{{location}}/memberships/{{membership_id}}/bindings/{{membership_binding_id}}")
if err != nil {
return fmt.Errorf("Error constructing id: %s", err)
}
d.SetId(id)
// Use the resource in the operation response to populate
// identity fields and d.Id() before read
var opRes map[string]interface{}
err = GKEHub2OperationWaitTimeWithResponse(
config, res, &opRes, project, "Creating MembershipBinding", userAgent,
d.Timeout(schema.TimeoutCreate))
if err != nil {
// The resource didn't actually create
d.SetId("")
return fmt.Errorf("Error waiting to create MembershipBinding: %s", err)
}
if err := d.Set("name", flattenGKEHub2MembershipBindingName(opRes["name"], d, config)); err != nil {
return err
}
// This may have caused the ID to update - update it if so.
id, err = tpgresource.ReplaceVars(d, config, "projects/{{project}}/locations/{{location}}/memberships/{{membership_id}}/bindings/{{membership_binding_id}}")
if err != nil {
return fmt.Errorf("Error constructing id: %s", err)
}
d.SetId(id)
log.Printf("[DEBUG] Finished creating MembershipBinding %q: %#v", d.Id(), res)
return resourceGKEHub2MembershipBindingRead(d, meta)
}
func resourceGKEHub2MembershipBindingRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*transport_tpg.Config)
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
if err != nil {
return err
}
url, err := tpgresource.ReplaceVars(d, config, "{{GKEHub2BasePath}}projects/{{project}}/locations/{{location}}/memberships/{{membership_id}}/bindings/{{membership_binding_id}}")
if err != nil {
return err
}
billingProject := ""
project, err := tpgresource.GetProject(d, config)
if err != nil {
return fmt.Errorf("Error fetching project for MembershipBinding: %s", err)
}
billingProject = project
// err == nil indicates that the billing_project value was found
if bp, err := tpgresource.GetBillingProject(d, config); err == nil {
billingProject = bp
}
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: config,
Method: "GET",
Project: billingProject,
RawURL: url,
UserAgent: userAgent,
})
if err != nil {
return transport_tpg.HandleNotFoundError(err, d, fmt.Sprintf("GKEHub2MembershipBinding %q", d.Id()))
}
if err := d.Set("project", project); err != nil {
return fmt.Errorf("Error reading MembershipBinding: %s", err)
}
if err := d.Set("name", flattenGKEHub2MembershipBindingName(res["name"], d, config)); err != nil {
return fmt.Errorf("Error reading MembershipBinding: %s", err)
}
if err := d.Set("uid", flattenGKEHub2MembershipBindingUid(res["uid"], d, config)); err != nil {
return fmt.Errorf("Error reading MembershipBinding: %s", err)
}
if err := d.Set("scope", flattenGKEHub2MembershipBindingScope(res["scope"], d, config)); err != nil {
return fmt.Errorf("Error reading MembershipBinding: %s", err)
}
if err := d.Set("create_time", flattenGKEHub2MembershipBindingCreateTime(res["createTime"], d, config)); err != nil {
return fmt.Errorf("Error reading MembershipBinding: %s", err)
}
if err := d.Set("update_time", flattenGKEHub2MembershipBindingUpdateTime(res["updateTime"], d, config)); err != nil {
return fmt.Errorf("Error reading MembershipBinding: %s", err)
}
if err := d.Set("delete_time", flattenGKEHub2MembershipBindingDeleteTime(res["deleteTime"], d, config)); err != nil {
return fmt.Errorf("Error reading MembershipBinding: %s", err)
}
if err := d.Set("state", flattenGKEHub2MembershipBindingState(res["state"], d, config)); err != nil {
return fmt.Errorf("Error reading MembershipBinding: %s", err)
}
if err := d.Set("labels", flattenGKEHub2MembershipBindingLabels(res["labels"], d, config)); err != nil {
return fmt.Errorf("Error reading MembershipBinding: %s", err)
}
if err := d.Set("terraform_labels", flattenGKEHub2MembershipBindingTerraformLabels(res["labels"], d, config)); err != nil {
return fmt.Errorf("Error reading MembershipBinding: %s", err)
}
if err := d.Set("effective_labels", flattenGKEHub2MembershipBindingEffectiveLabels(res["labels"], d, config)); err != nil {
return fmt.Errorf("Error reading MembershipBinding: %s", err)
}
return nil
}
func resourceGKEHub2MembershipBindingUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*transport_tpg.Config)
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
if err != nil {
return err
}
billingProject := ""
project, err := tpgresource.GetProject(d, config)
if err != nil {
return fmt.Errorf("Error fetching project for MembershipBinding: %s", err)
}
billingProject = project
obj := make(map[string]interface{})
scopeProp, err := expandGKEHub2MembershipBindingScope(d.Get("scope"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("scope"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, scopeProp)) {
obj["scope"] = scopeProp
}
labelsProp, err := expandGKEHub2MembershipBindingEffectiveLabels(d.Get("effective_labels"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("effective_labels"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, labelsProp)) {
obj["labels"] = labelsProp
}
url, err := tpgresource.ReplaceVars(d, config, "{{GKEHub2BasePath}}projects/{{project}}/locations/{{location}}/memberships/{{membership_id}}/bindings/{{membership_binding_id}}")
if err != nil {
return err
}
log.Printf("[DEBUG] Updating MembershipBinding %q: %#v", d.Id(), obj)
updateMask := []string{}
if d.HasChange("scope") {
updateMask = append(updateMask, "scope")
}
if d.HasChange("effective_labels") {
updateMask = append(updateMask, "labels")
}
// updateMask is a URL parameter but not present in the schema, so ReplaceVars
// won't set it
url, err = transport_tpg.AddQueryParams(url, map[string]string{"updateMask": strings.Join(updateMask, ",")})
if err != nil {
return err
}
// err == nil indicates that the billing_project value was found
if bp, err := tpgresource.GetBillingProject(d, config); err == nil {
billingProject = bp
}
// if updateMask is empty we are not updating anything so skip the post
if len(updateMask) > 0 {
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: config,
Method: "PATCH",
Project: billingProject,
RawURL: url,
UserAgent: userAgent,
Body: obj,
Timeout: d.Timeout(schema.TimeoutUpdate),
})
if err != nil {
return fmt.Errorf("Error updating MembershipBinding %q: %s", d.Id(), err)
} else {
log.Printf("[DEBUG] Finished updating MembershipBinding %q: %#v", d.Id(), res)
}
err = GKEHub2OperationWaitTime(
config, res, project, "Updating MembershipBinding", userAgent,
d.Timeout(schema.TimeoutUpdate))
if err != nil {
return err
}
}
return resourceGKEHub2MembershipBindingRead(d, meta)
}
func resourceGKEHub2MembershipBindingDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*transport_tpg.Config)
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
if err != nil {
return err
}
billingProject := ""
project, err := tpgresource.GetProject(d, config)
if err != nil {
return fmt.Errorf("Error fetching project for MembershipBinding: %s", err)
}
billingProject = project
url, err := tpgresource.ReplaceVars(d, config, "{{GKEHub2BasePath}}projects/{{project}}/locations/{{location}}/memberships/{{membership_id}}/bindings/{{membership_binding_id}}")
if err != nil {
return err
}
var obj map[string]interface{}
log.Printf("[DEBUG] Deleting MembershipBinding %q", d.Id())
// err == nil indicates that the billing_project value was found
if bp, err := tpgresource.GetBillingProject(d, config); err == nil {
billingProject = bp
}
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: config,
Method: "DELETE",
Project: billingProject,
RawURL: url,
UserAgent: userAgent,
Body: obj,
Timeout: d.Timeout(schema.TimeoutDelete),
})
if err != nil {
return transport_tpg.HandleNotFoundError(err, d, "MembershipBinding")
}
err = GKEHub2OperationWaitTime(
config, res, project, "Deleting MembershipBinding", userAgent,
d.Timeout(schema.TimeoutDelete))
if err != nil {
return err
}
log.Printf("[DEBUG] Finished deleting MembershipBinding %q: %#v", d.Id(), res)
return nil
}
func resourceGKEHub2MembershipBindingImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
config := meta.(*transport_tpg.Config)
if err := tpgresource.ParseImportId([]string{
"^projects/(?P<project>[^/]+)/locations/(?P<location>[^/]+)/memberships/(?P<membership_id>[^/]+)/bindings/(?P<membership_binding_id>[^/]+)$",
"^(?P<project>[^/]+)/(?P<location>[^/]+)/(?P<membership_id>[^/]+)/(?P<membership_binding_id>[^/]+)$",
"^(?P<location>[^/]+)/(?P<membership_id>[^/]+)/(?P<membership_binding_id>[^/]+)$",
}, d, config); err != nil {
return nil, err
}
// Replace import id for the resource id
id, err := tpgresource.ReplaceVars(d, config, "projects/{{project}}/locations/{{location}}/memberships/{{membership_id}}/bindings/{{membership_binding_id}}")
if err != nil {
return nil, fmt.Errorf("Error constructing id: %s", err)
}
d.SetId(id)
return []*schema.ResourceData{d}, nil
}
func flattenGKEHub2MembershipBindingName(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}
func flattenGKEHub2MembershipBindingUid(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}
func flattenGKEHub2MembershipBindingScope(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
if v == nil {
return v
}
return tpgresource.ConvertSelfLinkToV1(v.(string))
}
func flattenGKEHub2MembershipBindingCreateTime(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}
func flattenGKEHub2MembershipBindingUpdateTime(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}
func flattenGKEHub2MembershipBindingDeleteTime(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}
func flattenGKEHub2MembershipBindingState(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
if v == nil {
return nil
}
original := v.(map[string]interface{})
if len(original) == 0 {
return nil
}
transformed := make(map[string]interface{})
transformed["code"] =
flattenGKEHub2MembershipBindingStateCode(original["code"], d, config)
return []interface{}{transformed}
}
func flattenGKEHub2MembershipBindingStateCode(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}
func flattenGKEHub2MembershipBindingLabels(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
if v == nil {
return v
}
transformed := make(map[string]interface{})
if l, ok := d.GetOkExists("labels"); ok {
for k := range l.(map[string]interface{}) {
transformed[k] = v.(map[string]interface{})[k]
}
}
return transformed
}
func flattenGKEHub2MembershipBindingTerraformLabels(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
if v == nil {
return v
}
transformed := make(map[string]interface{})
if l, ok := d.GetOkExists("terraform_labels"); ok {
for k := range l.(map[string]interface{}) {
transformed[k] = v.(map[string]interface{})[k]
}
}
return transformed
}
func flattenGKEHub2MembershipBindingEffectiveLabels(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}
func expandGKEHub2MembershipBindingScope(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}
func expandGKEHub2MembershipBindingEffectiveLabels(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (map[string]string, error) {
if v == nil {
return map[string]string{}, nil
}
m := make(map[string]string)
for k, val := range v.(map[string]interface{}) {
m[k] = val.(string)
}
return m, nil
}