|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | +// ---------------------------------------------------------------------------- |
| 4 | +// |
| 5 | +// *** AUTO GENERATED CODE *** Type: Handwritten *** |
| 6 | +// |
| 7 | +// ---------------------------------------------------------------------------- |
| 8 | +// |
| 9 | +// This code is generated by Magic Modules using the following: |
| 10 | +// |
| 11 | +// Source file: https://github.com/GoogleCloudPlatform/magic-modules/tree/main/mmv1/third_party/terraform/services/resourcemanager/ephemeral_google_client_config.go |
| 12 | +// |
| 13 | +// DO NOT EDIT this file directly. Any changes made to this file will be |
| 14 | +// overwritten during the next generation cycle. |
| 15 | +// |
| 16 | +// ---------------------------------------------------------------------------- |
| 17 | +package resourcemanager |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + |
| 23 | + "github.com/hashicorp/terraform-plugin-framework/ephemeral" |
| 24 | + "github.com/hashicorp/terraform-plugin-framework/ephemeral/schema" |
| 25 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 26 | + transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport" |
| 27 | +) |
| 28 | + |
| 29 | +// Ensure the ephemeral resource satisfies the expected interfaces. |
| 30 | +var ( |
| 31 | + _ ephemeral.EphemeralResource = &GoogleClientConfigEphemeralResource{} |
| 32 | + _ ephemeral.EphemeralResourceWithConfigure = &GoogleClientConfigEphemeralResource{} |
| 33 | +) |
| 34 | + |
| 35 | +func GoogleEphemeralClientConfig() ephemeral.EphemeralResource { |
| 36 | + return &GoogleClientConfigEphemeralResource{} |
| 37 | +} |
| 38 | + |
| 39 | +type GoogleClientConfigEphemeralResource struct { |
| 40 | + providerConfig *transport_tpg.Config |
| 41 | +} |
| 42 | + |
| 43 | +type GoogleClientConfigEphemeralModel struct { |
| 44 | + // Id could/should be removed in future as it's not necessary in the plugin framework |
| 45 | + // https://github.com/hashicorp/terraform-plugin-testing/issues/84 |
| 46 | + Id types.String `tfsdk:"id"` |
| 47 | + Project types.String `tfsdk:"project"` |
| 48 | + Region types.String `tfsdk:"region"` |
| 49 | + Zone types.String `tfsdk:"zone"` |
| 50 | + AccessToken types.String `tfsdk:"access_token"` |
| 51 | + DefaultLabels types.Map `tfsdk:"default_labels"` |
| 52 | +} |
| 53 | + |
| 54 | +func (e *GoogleClientConfigEphemeralResource) Metadata(ctx context.Context, req ephemeral.MetadataRequest, resp *ephemeral.MetadataResponse) { |
| 55 | + resp.TypeName = req.ProviderTypeName + "_client_config" |
| 56 | +} |
| 57 | + |
| 58 | +func (e *GoogleClientConfigEphemeralResource) Schema(ctx context.Context, req ephemeral.SchemaRequest, resp *ephemeral.SchemaResponse) { |
| 59 | + |
| 60 | + resp.Schema = schema.Schema{ |
| 61 | + |
| 62 | + Description: "Use this ephemeral resource to access the configuration of the Google Cloud provider.", |
| 63 | + MarkdownDescription: "Use this ephemeral resource to access the configuration of the Google Cloud provider.", |
| 64 | + Attributes: map[string]schema.Attribute{ |
| 65 | + "id": schema.StringAttribute{ |
| 66 | + Computed: true, |
| 67 | + Description: "The ID of this ephemeral resource in Terraform state. It is created in a projects/{{project}}/regions/{{region}}/zones/{{zone}} format and is NOT used by the ephemeral resource in requests to Google APIs.", |
| 68 | + MarkdownDescription: "The ID of this ephemeral resource in Terraform state. It is created in a projects/{{project}}/regions/{{region}}/zones/{{zone}} format and is NOT used by the ephemeral resource in requests to Google APIs.", |
| 69 | + }, |
| 70 | + "project": schema.StringAttribute{ |
| 71 | + Description: "The ID of the project to apply any resources to.", |
| 72 | + MarkdownDescription: "The ID of the project to apply any resources to.", |
| 73 | + Computed: true, |
| 74 | + }, |
| 75 | + "region": schema.StringAttribute{ |
| 76 | + Description: "The region to operate under.", |
| 77 | + MarkdownDescription: "The region to operate under.", |
| 78 | + Computed: true, |
| 79 | + }, |
| 80 | + "zone": schema.StringAttribute{ |
| 81 | + Description: "The zone to operate under.", |
| 82 | + MarkdownDescription: "The zone to operate under.", |
| 83 | + Computed: true, |
| 84 | + }, |
| 85 | + "access_token": schema.StringAttribute{ |
| 86 | + Description: "The OAuth2 access token used by the client to authenticate against the Google Cloud API.", |
| 87 | + MarkdownDescription: "The OAuth2 access token used by the client to authenticate against the Google Cloud API.", |
| 88 | + Computed: true, |
| 89 | + Sensitive: true, |
| 90 | + }, |
| 91 | + "default_labels": schema.MapAttribute{ |
| 92 | + Description: "The default labels configured on the provider.", |
| 93 | + MarkdownDescription: "The default labels configured on the provider.", |
| 94 | + Computed: true, |
| 95 | + ElementType: types.StringType, |
| 96 | + }, |
| 97 | + }, |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +func (e *GoogleClientConfigEphemeralResource) Configure(ctx context.Context, req ephemeral.ConfigureRequest, resp *ephemeral.ConfigureResponse) { |
| 102 | + // Prevent panic if the provider has not been configured. |
| 103 | + if req.ProviderData == nil { |
| 104 | + return |
| 105 | + } |
| 106 | + |
| 107 | + p, ok := req.ProviderData.(*transport_tpg.Config) |
| 108 | + if !ok { |
| 109 | + resp.Diagnostics.AddError( |
| 110 | + "Unexpected Ephemeral Resource Configure Type", |
| 111 | + fmt.Sprintf("Expected *transport_tpg.Config, got: %T. Please report this issue to the provider developers.", req.ProviderData), |
| 112 | + ) |
| 113 | + return |
| 114 | + } |
| 115 | + |
| 116 | + // Required for accessing project, region, zone and tokenSource |
| 117 | + e.providerConfig = p |
| 118 | +} |
| 119 | + |
| 120 | +func (e *GoogleClientConfigEphemeralResource) Open(ctx context.Context, req ephemeral.OpenRequest, resp *ephemeral.OpenResponse) { |
| 121 | + var data GoogleClientConfigEphemeralModel |
| 122 | + |
| 123 | + // Read Terraform configuration data into the model |
| 124 | + resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) |
| 125 | + if resp.Diagnostics.HasError() { |
| 126 | + return |
| 127 | + } |
| 128 | + |
| 129 | + data.Id = types.StringValue(fmt.Sprintf("projects/%s/regions/%s/zones/%s", e.providerConfig.Project, e.providerConfig.Region, e.providerConfig.Zone)) |
| 130 | + data.Project = types.StringValue(e.providerConfig.Project) |
| 131 | + data.Region = types.StringValue(e.providerConfig.Region) |
| 132 | + data.Zone = types.StringValue(e.providerConfig.Zone) |
| 133 | + |
| 134 | + dls, diags := types.MapValueFrom(ctx, types.StringType, e.providerConfig.DefaultLabels) |
| 135 | + resp.Diagnostics.Append(diags...) |
| 136 | + if resp.Diagnostics.HasError() { |
| 137 | + return |
| 138 | + } |
| 139 | + |
| 140 | + data.DefaultLabels = dls |
| 141 | + |
| 142 | + token, err := e.providerConfig.TokenSource.Token() |
| 143 | + if err != nil { |
| 144 | + resp.Diagnostics.AddError("Error setting access_token", err.Error()) |
| 145 | + return |
| 146 | + } |
| 147 | + data.AccessToken = types.StringValue(token.AccessToken) |
| 148 | + |
| 149 | + // Save data into ephemeral resource result |
| 150 | + resp.Diagnostics.Append(resp.Result.Set(ctx, &data)...) |
| 151 | +} |
0 commit comments