|
| 1 | +package datasource |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "terraform-provider-plural/internal/client" |
| 8 | + "terraform-provider-plural/internal/common" |
| 9 | + "terraform-provider-plural/internal/model" |
| 10 | + |
| 11 | + "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/datasource" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" |
| 14 | + "github.com/hashicorp/terraform-plugin-framework/path" |
| 15 | + "github.com/hashicorp/terraform-plugin-framework/schema/validator" |
| 16 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 17 | +) |
| 18 | + |
| 19 | +func NewCloudConnectionDataSource() datasource.DataSource { |
| 20 | + return &cloudConnectionDataSource{} |
| 21 | +} |
| 22 | + |
| 23 | +type cloudConnectionDataSource struct { |
| 24 | + client *client.Client |
| 25 | +} |
| 26 | + |
| 27 | +func (d *cloudConnectionDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { |
| 28 | + resp.TypeName = req.ProviderTypeName + "_cloud_connection" |
| 29 | +} |
| 30 | + |
| 31 | +func (d *cloudConnectionDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { |
| 32 | + resp.Schema = schema.Schema{ |
| 33 | + Attributes: map[string]schema.Attribute{ |
| 34 | + "id": schema.StringAttribute{ |
| 35 | + Description: "Internal identifier of this cloud connection", |
| 36 | + MarkdownDescription: "Internal identifier of this cloud connection", |
| 37 | + Optional: true, |
| 38 | + Computed: true, |
| 39 | + Validators: []validator.String{stringvalidator.ExactlyOneOf(path.MatchRoot("name"))}, |
| 40 | + }, |
| 41 | + "name": schema.StringAttribute{ |
| 42 | + Description: "Human-readable name of this cloud connection.", |
| 43 | + MarkdownDescription: "Human-readable name of this cloud connection.", |
| 44 | + Optional: true, |
| 45 | + Computed: true, |
| 46 | + Validators: []validator.String{stringvalidator.ExactlyOneOf(path.MatchRoot("id"))}, |
| 47 | + }, |
| 48 | + "cloud_provider": schema.StringAttribute{ |
| 49 | + Description: "The cloud provider of this cloud connection.", |
| 50 | + MarkdownDescription: "The cloud provider of this cloud connection.", |
| 51 | + Required: true, |
| 52 | + Validators: []validator.String{stringvalidator.OneOf("AWS", "GCP", "AZURE")}, |
| 53 | + }, |
| 54 | + "configuration": schema.SingleNestedAttribute{ |
| 55 | + Description: "Cloud provider configuration", |
| 56 | + MarkdownDescription: "Cloud provider configuration", |
| 57 | + Required: true, |
| 58 | + }, |
| 59 | + "read_bindings": schema.SetAttribute{ |
| 60 | + Description: "The read bindings for this cloud connection.", |
| 61 | + MarkdownDescription: "The read bindings for this cloud connection.", |
| 62 | + Optional: true, |
| 63 | + ElementType: types.ObjectType{AttrTypes: common.PolicyBindingAttrTypes}, |
| 64 | + }, |
| 65 | + }, |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +func (d *cloudConnectionDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { |
| 70 | + if req.ProviderData == nil { |
| 71 | + return |
| 72 | + } |
| 73 | + |
| 74 | + data, ok := req.ProviderData.(*common.ProviderData) |
| 75 | + if !ok { |
| 76 | + resp.Diagnostics.AddError( |
| 77 | + "Unexpected Cloud Connection Resource Configure Type", |
| 78 | + fmt.Sprintf("Expected *common.ProviderData, got: %T. Please report this issue to the provider developers.", req.ProviderData), |
| 79 | + ) |
| 80 | + return |
| 81 | + } |
| 82 | + |
| 83 | + d.client = data.Client |
| 84 | +} |
| 85 | + |
| 86 | +func (d *cloudConnectionDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { |
| 87 | + data := new(model.CloudConnection) |
| 88 | + resp.Diagnostics.Append(req.Config.Get(ctx, data)...) |
| 89 | + if resp.Diagnostics.HasError() { |
| 90 | + return |
| 91 | + } |
| 92 | + |
| 93 | + if data.Id.IsNull() && data.Name.IsNull() { |
| 94 | + resp.Diagnostics.AddError( |
| 95 | + "Missing Cloud Connection ID and Name", |
| 96 | + "The provider could not read cloud connection data. ID or name needs to be specified.", |
| 97 | + ) |
| 98 | + return |
| 99 | + } |
| 100 | + |
| 101 | + response, err := d.client.GetCloudConnection(ctx, data.Id.ValueStringPointer(), data.Name.ValueStringPointer()) |
| 102 | + if err != nil { |
| 103 | + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read cloud connection, got error: %s", err)) |
| 104 | + return |
| 105 | + } |
| 106 | + |
| 107 | + data.From(response.CloudConnection, ctx, &resp.Diagnostics) |
| 108 | + resp.Diagnostics.Append(resp.State.Set(ctx, data)...) |
| 109 | +} |
0 commit comments