generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 44
/
data_source_variables.go
110 lines (96 loc) · 2.74 KB
/
data_source_variables.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
101
102
103
104
105
106
107
108
109
110
package variables
import (
"context"
"errors"
"net/http"
"github.com/harness/harness-go-sdk/harness/nextgen"
"github.com/harness/terraform-provider-harness/helpers"
"github.com/harness/terraform-provider-harness/internal"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func DataSourceVariables() *schema.Resource {
resource := &schema.Resource{
Description: "Data source for retrieving a Harness Variable.",
ReadContext: dataSourceVariablesRead,
Schema: map[string]*schema.Schema{
"identifier": {
Description: "Unique identifier of the resource",
Type: schema.TypeString,
Required: true,
},
"name": {
Description: "Name of the Variable",
Type: schema.TypeString,
Computed: true,
},
"description": {
Description: "Description of the entity",
Type: schema.TypeString,
Computed: true,
},
"org_id": {
Description: "Organization Identifier for the Entity",
Type: schema.TypeString,
Optional: true,
},
"project_id": {
Description: "Project Identifier for the Entity",
Type: schema.TypeString,
Optional: true,
},
"type": {
Description: "Type of Variable",
Type: schema.TypeString,
Computed: true,
},
"spec": {
Description: "List of Spce Fields.",
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"value_type": {
Description: "Type of Value of the Variable. For now only FIXED is supported",
Type: schema.TypeString,
Computed: true,
},
"fixed_value": {
Description: "FixedValue of the variable",
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
return resource
}
func dataSourceVariablesRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c, ctx := meta.(*internal.Session).GetPlatformClientWithContext(ctx)
var variable *nextgen.VariableDto
var err error
var httpResp *http.Response
id := d.Get("identifier").(string)
if id != "" {
var resp nextgen.ResponseDtoVariableResponseDto
resp, httpResp, err = c.VariablesApi.GetVariable(ctx, id, c.AccountId, &nextgen.VariablesApiGetVariableOpts{
OrgIdentifier: helpers.BuildField(d, "org_id"),
ProjectIdentifier: helpers.BuildField(d, "project_id"),
})
variable = resp.Data.Variable
} else {
return diag.FromErr(errors.New(" identifier must be specified"))
}
if err != nil {
return helpers.HandleApiError(err, d, httpResp)
}
if variable == nil {
d.SetId("")
d.MarkNewResource()
return nil
}
readVariable(d, variable)
return nil
}