|
| 1 | +package extended_attributes |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/dbt-labs/terraform-provider-dbtcloud/pkg/dbt_cloud" |
| 8 | + "github.com/hashicorp/terraform-plugin-framework/datasource" |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 10 | +) |
| 11 | + |
| 12 | +var ( |
| 13 | + _ datasource.DataSource = &extendedAttributesDataSource{} |
| 14 | + _ datasource.DataSourceWithConfigure = &extendedAttributesDataSource{} |
| 15 | +) |
| 16 | + |
| 17 | +func ExtendedAttributesDataSource() datasource.DataSource { |
| 18 | + return &extendedAttributesDataSource{} |
| 19 | +} |
| 20 | + |
| 21 | +type extendedAttributesDataSource struct { |
| 22 | + client *dbt_cloud.Client |
| 23 | +} |
| 24 | + |
| 25 | +func (p *extendedAttributesDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { |
| 26 | + if req.ProviderData == nil { |
| 27 | + return |
| 28 | + } |
| 29 | + |
| 30 | + client, ok := req.ProviderData.(*dbt_cloud.Client) |
| 31 | + if !ok { |
| 32 | + resp.Diagnostics.AddError( |
| 33 | + "Unexpected Data Source Configure Type", |
| 34 | + fmt.Sprintf("Expected *dbt_cloud.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData), |
| 35 | + ) |
| 36 | + return |
| 37 | + } |
| 38 | + |
| 39 | + p.client = client |
| 40 | +} |
| 41 | + |
| 42 | +func (p *extendedAttributesDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { |
| 43 | + resp.TypeName = req.ProviderTypeName + "_extended_attributes" |
| 44 | +} |
| 45 | + |
| 46 | +func (p *extendedAttributesDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { |
| 47 | + var state ExtendedAttributesDataSourceModel |
| 48 | + diags := req.Config.Get(ctx, &state) |
| 49 | + resp.Diagnostics.Append(diags...) |
| 50 | + if resp.Diagnostics.HasError() { |
| 51 | + return |
| 52 | + } |
| 53 | + |
| 54 | + projectId := int(state.ProjectID.ValueInt64()) |
| 55 | + extendedAttributesId := int(state.ExtendedAttributesID.ValueInt64()) |
| 56 | + |
| 57 | + extendedAttributes, err := p.client.GetExtendedAttributes(projectId, extendedAttributesId) |
| 58 | + if err != nil { |
| 59 | + resp.Diagnostics.AddError( |
| 60 | + "Error reading Extended attributes", |
| 61 | + "Could not read Extended attributes ID "+state.ID.ValueString()+": "+err.Error(), |
| 62 | + ) |
| 63 | + return |
| 64 | + } |
| 65 | + |
| 66 | + state.ID = types.StringValue(fmt.Sprintf("%d%s%d", *extendedAttributes.ID, dbt_cloud.ID_DELIMITER, *extendedAttributes.ID)) |
| 67 | + state.ProjectID = types.Int64Value(int64(extendedAttributes.ProjectID)) |
| 68 | + state.ExtendedAttributesID = types.Int64Value(int64(*extendedAttributes.ID)) |
| 69 | + state.ExtendedAttributes = types.StringValue(string(extendedAttributes.ExtendedAttributes)) |
| 70 | + state.State = types.Int64Value(int64(extendedAttributes.State)) |
| 71 | + |
| 72 | + diags = resp.State.Set(ctx, &state) |
| 73 | + resp.Diagnostics.Append(diags...) |
| 74 | + if resp.Diagnostics.HasError() { |
| 75 | + return |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +func (p *extendedAttributesDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { |
| 80 | + resp.Schema = dataSourceSchema |
| 81 | +} |
0 commit comments