|
| 1 | +package hsdp |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 8 | +) |
| 9 | + |
| 10 | +func dataSourceCDLLabelDefinition() *schema.Resource { |
| 11 | + return &schema.Resource{ |
| 12 | + ReadContext: dataSourceCDLLabelDefinitionRead, |
| 13 | + Schema: map[string]*schema.Schema{ |
| 14 | + "cdl_endpoint": { |
| 15 | + Type: schema.TypeString, |
| 16 | + Required: true, |
| 17 | + }, |
| 18 | + "label_def_id": { |
| 19 | + Type: schema.TypeString, |
| 20 | + Required: true, |
| 21 | + }, |
| 22 | + "study_id": { |
| 23 | + Type: schema.TypeString, |
| 24 | + Required: true, |
| 25 | + }, |
| 26 | + "label_def_name": { |
| 27 | + Type: schema.TypeString, |
| 28 | + Computed: true, |
| 29 | + }, |
| 30 | + "description": { |
| 31 | + Type: schema.TypeString, |
| 32 | + Computed: true, |
| 33 | + }, |
| 34 | + "label_scope": { |
| 35 | + Type: schema.TypeString, |
| 36 | + Computed: true, |
| 37 | + }, |
| 38 | + "label_name": { |
| 39 | + Type: schema.TypeString, |
| 40 | + Computed: true, |
| 41 | + }, |
| 42 | + "type": { |
| 43 | + Type: schema.TypeString, |
| 44 | + Computed: true, |
| 45 | + }, |
| 46 | + "labels": { |
| 47 | + Type: schema.TypeString, |
| 48 | + Computed: true, |
| 49 | + }, |
| 50 | + "created_by": { |
| 51 | + Type: schema.TypeString, |
| 52 | + Computed: true, |
| 53 | + }, |
| 54 | + "created_on": { |
| 55 | + Type: schema.TypeString, |
| 56 | + Computed: true, |
| 57 | + }, |
| 58 | + }, |
| 59 | + } |
| 60 | + |
| 61 | +} |
| 62 | + |
| 63 | +func dataSourceCDLLabelDefinitionRead(_ context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 64 | + config := m.(*Config) |
| 65 | + |
| 66 | + var diags diag.Diagnostics |
| 67 | + |
| 68 | + labelDefId := d.Get("label_def_id").(string) |
| 69 | + endpoint := d.Get("cdl_endpoint").(string) |
| 70 | + studyId := d.Get("study_id").(string) |
| 71 | + |
| 72 | + client, err := config.getCDLClientFromEndpoint(endpoint) |
| 73 | + if err != nil { |
| 74 | + return diag.FromErr(err) |
| 75 | + } |
| 76 | + defer client.Close() |
| 77 | + |
| 78 | + labelDefinition, _, err := client.LabelDefinition.GetLabelDefinitionByID(studyId, labelDefId) |
| 79 | + if err != nil { |
| 80 | + return diag.FromErr(err) |
| 81 | + } |
| 82 | + |
| 83 | + labelScopeBytes, err := json.Marshal((*labelDefinition).LabelScope) |
| 84 | + if err != nil { |
| 85 | + return diag.FromErr(err) |
| 86 | + } |
| 87 | + |
| 88 | + labelsBytes, err := json.Marshal((*labelDefinition).Labels) |
| 89 | + if err != nil { |
| 90 | + return diag.FromErr(err) |
| 91 | + } |
| 92 | + |
| 93 | + d.SetId((*labelDefinition).ID) |
| 94 | + _ = d.Set("label_def_name", (*labelDefinition).LabelDefName) |
| 95 | + _ = d.Set("description", (*labelDefinition).Description) |
| 96 | + _ = d.Set("label_scope", string(labelScopeBytes)) |
| 97 | + _ = d.Set("label_name", (*labelDefinition).Label) |
| 98 | + _ = d.Set("type", (*labelDefinition).Type) |
| 99 | + _ = d.Set("labels", string(labelsBytes)) |
| 100 | + _ = d.Set("created_by", (*labelDefinition).CreatedBy) |
| 101 | + _ = d.Set("created_on", (*labelDefinition).CreatedOn) |
| 102 | + |
| 103 | + return diags |
| 104 | +} |
0 commit comments