generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 41
/
environment_data_source.go
124 lines (111 loc) · 3.39 KB
/
environment_data_source.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package environment
import (
"context"
"github.com/harness/harness-go-sdk/harness/cd/cac"
"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 DataSourceEnvironment() *schema.Resource {
return &schema.Resource{
// This description is used by the documentation generator and the language server.
Description: "Data source for retrieving a Harness service",
ReadContext: dataSourceEnvironmentRead,
Schema: map[string]*schema.Schema{
"id": {
Description: "The id of the environment.",
Type: schema.TypeString,
Computed: true,
},
"environment_id": {
Description: "The id of the environment.",
Type: schema.TypeString,
Optional: true,
AtLeastOneOf: []string{"environment_id", "name"},
ConflictsWith: []string{"name"},
},
"app_id": {
Description: "The id of the application.",
Type: schema.TypeString,
Required: true,
},
"name": {
Description: "The name of the environment.",
Type: schema.TypeString,
Optional: true,
AtLeastOneOf: []string{"environment_id", "name"},
ConflictsWith: []string{"environment_id"},
},
"description": {
Description: "The description of the environment.",
Type: schema.TypeString,
Computed: true,
},
"type": {
Description: "The type of the environment. Valid values are `PROD` and `NON_PROD`",
Type: schema.TypeString,
Computed: true,
},
"variable_override": {
Description: "Override for a service variable",
Type: schema.TypeSet,
Computed: true,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Description: "The name of the variable",
Type: schema.TypeString,
Computed: true,
},
"service_name": {
Description: "The name of the service",
Type: schema.TypeString,
Computed: true,
},
"value": {
Description: "The value of the service variable",
Type: schema.TypeString,
Computed: true,
},
"type": {
Description: "The type of the service variable. Valid values are `TEXT` and `ENCRYPTED_TEXT`",
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}
func dataSourceEnvironmentRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*internal.Session)
var env *cac.Environment
var err error
appId := d.Get("app_id").(string)
if id := d.Get("environment_id").(string); id != "" {
env, err = c.CDClient.ConfigAsCodeClient.GetEnvironmentById(appId, id)
if err != nil {
return diag.FromErr(err)
}
} else if name := d.Get("name").(string); name != "" {
env, err = c.CDClient.ConfigAsCodeClient.GetEnvironmentByName(appId, name)
if err != nil {
return diag.FromErr(err)
}
}
if env == nil {
return diag.Errorf("environment not found")
}
d.SetId(env.Id)
d.Set("environment_id", env.Id)
d.Set("app_id", env.ApplicationId)
d.Set("name", env.Name)
d.Set("type", env.EnvironmentType)
d.Set("description", env.Description)
if overrides := flattenVariableOverrides(env.VariableOverrides); len(overrides) > 0 {
d.Set("variable_overrides", overrides)
}
return nil
}