|
| 1 | +package data_sources |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "strconv" |
| 6 | + |
| 7 | + "github.com/dbt-labs/terraform-provider-dbtcloud/pkg/dbt_cloud" |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 10 | +) |
| 11 | + |
| 12 | +var extendedAttributesSchema = map[string]*schema.Schema{ |
| 13 | + "extended_attributes_id": &schema.Schema{ |
| 14 | + Type: schema.TypeInt, |
| 15 | + Required: true, |
| 16 | + Description: "ID of the extended attributes", |
| 17 | + }, |
| 18 | + "project_id": &schema.Schema{ |
| 19 | + Type: schema.TypeInt, |
| 20 | + Required: true, |
| 21 | + Description: "Project ID the extended attributes refers to", |
| 22 | + }, |
| 23 | + "state": &schema.Schema{ |
| 24 | + Type: schema.TypeInt, |
| 25 | + Computed: true, |
| 26 | + Description: "The state of the extended attributes (1 = active, 2 = inactive)", |
| 27 | + }, |
| 28 | + "extended_attributes": &schema.Schema{ |
| 29 | + Type: schema.TypeString, |
| 30 | + Computed: true, |
| 31 | + Description: "A JSON string listing the extended attributes mapping", |
| 32 | + }, |
| 33 | +} |
| 34 | + |
| 35 | +func DatasourceExtendedAttributes() *schema.Resource { |
| 36 | + return &schema.Resource{ |
| 37 | + ReadContext: datasourceExtendedAttributesRead, |
| 38 | + Schema: extendedAttributesSchema, |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func datasourceExtendedAttributesRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 43 | + c := m.(*dbt_cloud.Client) |
| 44 | + |
| 45 | + var diags diag.Diagnostics |
| 46 | + |
| 47 | + extendedAttributesID := d.Get("extended_attributes_id").(int) |
| 48 | + projectID := d.Get("project_id").(int) |
| 49 | + |
| 50 | + extendedAttributes, err := c.GetExtendedAttributes(projectID, extendedAttributesID) |
| 51 | + if err != nil { |
| 52 | + return diag.FromErr(err) |
| 53 | + } |
| 54 | + |
| 55 | + if err := d.Set("extended_attributes_id", extendedAttributes.ID); err != nil { |
| 56 | + return diag.FromErr(err) |
| 57 | + } |
| 58 | + if err := d.Set("state", extendedAttributes.State); err != nil { |
| 59 | + return diag.FromErr(err) |
| 60 | + } |
| 61 | + if err := d.Set("project_id", extendedAttributes.ProjectID); err != nil { |
| 62 | + return diag.FromErr(err) |
| 63 | + } |
| 64 | + if err := d.Set("extended_attributes", string(extendedAttributes.ExtendedAttributes)); err != nil { |
| 65 | + return diag.FromErr(err) |
| 66 | + } |
| 67 | + |
| 68 | + d.SetId(strconv.Itoa(*extendedAttributes.ID)) |
| 69 | + |
| 70 | + return diags |
| 71 | +} |
0 commit comments