-
Notifications
You must be signed in to change notification settings - Fork 148
/
resource_tfe_organization.go
206 lines (169 loc) · 6.05 KB
/
resource_tfe_organization.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package tfe
import (
"fmt"
"log"
"strings"
tfe "github.com/hashicorp/go-tfe"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
func resourceTFEOrganization() *schema.Resource {
return &schema.Resource{
Create: resourceTFEOrganizationCreate,
Read: resourceTFEOrganizationRead,
Update: resourceTFEOrganizationUpdate,
Delete: resourceTFEOrganizationDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
return strings.EqualFold(old, new)
},
},
"email": {
Type: schema.TypeString,
Required: true,
},
"session_timeout_minutes": {
Type: schema.TypeInt,
Optional: true,
},
"session_remember_minutes": {
Type: schema.TypeInt,
Optional: true,
},
"collaborator_auth_policy": {
Type: schema.TypeString,
Optional: true,
Default: string(tfe.AuthPolicyPassword),
ValidateFunc: validation.StringInSlice(
[]string{
string(tfe.AuthPolicyPassword),
string(tfe.AuthPolicyTwoFactor),
},
false,
),
},
"owners_team_saml_role_id": {
Type: schema.TypeString,
Optional: true,
},
"cost_estimation_enabled": {
Type: schema.TypeBool,
Optional: true,
Computed: true,
},
"send_passing_statuses_for_untriggered_speculative_plans": {
Type: schema.TypeBool,
Optional: true,
Computed: true,
},
"assessments_enforced": {
Type: schema.TypeBool,
Optional: true,
},
},
}
}
func resourceTFEOrganizationCreate(d *schema.ResourceData, meta interface{}) error {
tfeClient := meta.(*tfe.Client)
// Get the organization name.
name := d.Get("name").(string)
// Create a new options struct.
options := tfe.OrganizationCreateOptions{
Name: tfe.String(name),
Email: tfe.String(d.Get("email").(string)),
}
log.Printf("[DEBUG] Create new organization: %s", name)
org, err := tfeClient.Organizations.Create(ctx, options)
if err != nil {
return fmt.Errorf("Error creating the new organization %s: %w", name, err)
}
d.SetId(org.Name)
return resourceTFEOrganizationUpdate(d, meta)
}
func resourceTFEOrganizationRead(d *schema.ResourceData, meta interface{}) error {
tfeClient := meta.(*tfe.Client)
log.Printf("[DEBUG] Read configuration of organization: %s", d.Id())
org, err := tfeClient.Organizations.Read(ctx, d.Id())
if err != nil {
if err == tfe.ErrResourceNotFound {
log.Printf("[DEBUG] Organization %s does no longer exist", d.Id())
d.SetId("")
return nil
}
return err
}
// Update the config.
d.Set("name", org.Name)
d.Set("email", org.Email)
d.Set("session_timeout_minutes", org.SessionTimeout)
d.Set("session_remember_minutes", org.SessionRemember)
d.Set("collaborator_auth_policy", org.CollaboratorAuthPolicy)
d.Set("owners_team_saml_role_id", org.OwnersTeamSAMLRoleID)
d.Set("cost_estimation_enabled", org.CostEstimationEnabled)
d.Set("send_passing_statuses_for_untriggered_speculative_plans", org.SendPassingStatusesForUntriggeredSpeculativePlans)
// TFE (onprem) does not currently have this feature and this value won't be returned in those cases.
// org.AssessmentsEnforced will default to false
d.Set("assessments_enforced", org.AssessmentsEnforced)
return nil
}
func resourceTFEOrganizationUpdate(d *schema.ResourceData, meta interface{}) error {
tfeClient := meta.(*tfe.Client)
// Create a new options struct.
options := tfe.OrganizationUpdateOptions{
Name: tfe.String(d.Get("name").(string)),
Email: tfe.String(d.Get("email").(string)),
}
// If session_timeout is supplied, set it using the options struct.
if sessionTimeout, ok := d.GetOk("session_timeout_minutes"); ok {
options.SessionTimeout = tfe.Int(sessionTimeout.(int))
}
// If session_remember is supplied, set it using the options struct.
if sessionRemember, ok := d.GetOk("session_remember_minutes"); ok {
options.SessionRemember = tfe.Int(sessionRemember.(int))
}
// If collaborator_auth_policy is supplied, set it using the options struct.
if authPolicy, ok := d.GetOk("collaborator_auth_policy"); ok {
options.CollaboratorAuthPolicy = tfe.AuthPolicy(tfe.AuthPolicyType(authPolicy.(string)))
}
// If owners_team_saml_role_id is supplied, set it using the options struct.
if ownersTeamSAMLRoleID, ok := d.GetOk("owners_team_saml_role_id"); ok {
options.OwnersTeamSAMLRoleID = tfe.String(ownersTeamSAMLRoleID.(string))
}
// If cost_estimation_enabled is supplied, set it using the options struct.
if costEstimationEnabled, ok := d.GetOkExists("cost_estimation_enabled"); ok {
options.CostEstimationEnabled = tfe.Bool(costEstimationEnabled.(bool))
}
// If send_passing_statuses_for_untriggered_speculative_plans is supplied, set it using the options struct.
if sendPassingStatusesForUntriggeredSpeculativePlans, ok := d.GetOk("send_passing_statuses_for_untriggered_speculative_plans"); ok {
options.SendPassingStatusesForUntriggeredSpeculativePlans = tfe.Bool(sendPassingStatusesForUntriggeredSpeculativePlans.(bool))
}
// If cost_estimation_enabled is supplied, set it using the options struct.
if assessmentsEnforced, ok := d.GetOkExists("assessments_enforced"); ok {
options.AssessmentsEnforced = tfe.Bool(assessmentsEnforced.(bool))
}
log.Printf("[DEBUG] Update configuration of organization: %s", d.Id())
org, err := tfeClient.Organizations.Update(ctx, d.Id(), options)
if err != nil {
return fmt.Errorf("Error updating organization %s: %w", d.Id(), err)
}
d.SetId(org.Name)
return resourceTFEOrganizationRead(d, meta)
}
func resourceTFEOrganizationDelete(d *schema.ResourceData, meta interface{}) error {
tfeClient := meta.(*tfe.Client)
log.Printf("[DEBUG] Delete organization: %s", d.Id())
err := tfeClient.Organizations.Delete(ctx, d.Id())
if err != nil {
if err == tfe.ErrResourceNotFound {
return nil
}
return fmt.Errorf("Error deleting organization %s: %w", d.Id(), err)
}
return nil
}