/
data_source_aws_credential_prerequisites.go
100 lines (80 loc) · 3.8 KB
/
data_source_aws_credential_prerequisites.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright 2023 Cloudera. All Rights Reserved.
//
// This file is licensed under the Apache License Version 2.0 (the "License").
// You may not use this file except in compliance with the License.
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
//
// This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
// OF ANY KIND, either express or implied. Refer to the License for the specific
// permissions and limitations governing your use of the file.
package environments
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/cdp"
"github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/gen/environments/client/operations"
environmentsmodels "github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/gen/environments/models"
"github.com/cloudera/terraform-provider-cdp/utils"
)
// Ensure the implementation satisfies the expected interfaces.
var (
_ datasource.DataSourceWithConfigure = &awsCredentialPrerequisitesDataSource{}
)
func NewAWSCredentialPrerequisitesDataSource() datasource.DataSource {
return &awsCredentialPrerequisitesDataSource{}
}
type awsCredentialPrerequisitesDataSource struct {
client *cdp.Client
}
// Configure adds the provider configured client to the data source.
func (d *awsCredentialPrerequisitesDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
d.client = utils.GetCdpClientForDataSource(req, resp)
}
func (d *awsCredentialPrerequisitesDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_environments_aws_credential_prerequisites"
}
// Read refreshes the Terraform state with the latest data.
func (d *awsCredentialPrerequisitesDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data awsCredentialPrerequisitesDataSourceModel
// Read Terraform configuration data into the model
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
tflog.Info(ctx, "Reading GetCredentialPrerequisites")
client := d.client.Environments
cloudPlatform := "AWS"
params := operations.NewGetCredentialPrerequisitesParamsWithContext(ctx)
params.WithInput(&environmentsmodels.GetCredentialPrerequisitesRequest{CloudPlatform: &cloudPlatform})
response, err := client.Operations.GetCredentialPrerequisites(params)
if err != nil {
msg := err.Error()
if d, ok := err.(*operations.GetCredentialPrerequisitesDefault); ok && d.GetPayload() != nil {
msg = d.GetPayload().Message
}
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read cdp_environments_aws_credential_prerequisites, got error: %s", msg))
return
}
prerequisites := response.GetPayload()
if prerequisites == nil || prerequisites.Aws == nil {
resp.State.RemoveResource(ctx) // deleted
return
}
ctx = tflog.SetField(ctx, "Client info:", client)
tflog.Info(ctx, "Read GetCredentialPrerequisites")
data.AccountID = types.StringValue(prerequisites.AccountID)
data.ExternalID = types.StringValue(*prerequisites.Aws.ExternalID)
data.ID = types.StringValue(prerequisites.AccountID + ":" + *prerequisites.Aws.ExternalID)
data.Policy = types.StringPointerValue(prerequisites.Aws.PolicyJSON)
var policyMap = make(map[string]string)
for _, policy := range prerequisites.Aws.Policies {
policyMap[*policy.Service] = *policy.PolicyJSON
}
policyTypeMap, convDiag := types.MapValueFrom(ctx, types.StringType, policyMap)
data.Policies = policyTypeMap
resp.Diagnostics.Append(convDiag...)
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}