This repository has been archived by the owner on Sep 14, 2021. It is now read-only.
forked from hashicorp/terraform-provider-tfe
/
resource_tfe_organization_token.go
117 lines (94 loc) · 3.18 KB
/
resource_tfe_organization_token.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
package tfe
import (
"fmt"
"log"
tfe "github.com/hashicorp/go-tfe"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceTFEOrganizationToken() *schema.Resource {
return &schema.Resource{
Create: resourceTFEOrganizationTokenCreate,
Read: resourceTFEOrganizationTokenRead,
Delete: resourceTFEOrganizationTokenDelete,
Importer: &schema.ResourceImporter{
State: resourceTFEOrganizationTokenImporter,
},
Schema: map[string]*schema.Schema{
"organization": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"force_regenerate": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
},
"token": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},
},
}
}
func resourceTFEOrganizationTokenCreate(d *schema.ResourceData, meta interface{}) error {
tfeClient := meta.(*tfe.Client)
// Get the organization name.
organization := d.Get("organization").(string)
log.Printf("[DEBUG] Check if a token already exists for organization: %s", organization)
_, err := tfeClient.OrganizationTokens.Read(ctx, organization)
if err != nil && err != tfe.ErrResourceNotFound {
return fmt.Errorf("Error checking if a token exists for organization %s: %v", organization, err)
}
// If error is nil, the token already exists.
if err == nil {
if !d.Get("force_regenerate").(bool) {
return fmt.Errorf("A token already exists for organization: %s", organization)
}
log.Printf("[DEBUG] Regenerating existing token for organization: %s", organization)
}
token, err := tfeClient.OrganizationTokens.Generate(ctx, organization)
if err != nil {
return fmt.Errorf(
"Error creating new token for organization %s: %v", organization, err)
}
d.SetId(organization)
// We need to set this here in the create function as this value will
// only be returned once during the creation of the token.
d.Set("token", token.Token)
return resourceTFEOrganizationTokenRead(d, meta)
}
func resourceTFEOrganizationTokenRead(d *schema.ResourceData, meta interface{}) error {
tfeClient := meta.(*tfe.Client)
log.Printf("[DEBUG] Read the token from organization: %s", d.Id())
_, err := tfeClient.OrganizationTokens.Read(ctx, d.Id())
if err != nil {
if err == tfe.ErrResourceNotFound {
log.Printf("[DEBUG] Token for organization %s does no longer exist", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("Error reading token from organization %s: %v", d.Id(), err)
}
return nil
}
func resourceTFEOrganizationTokenDelete(d *schema.ResourceData, meta interface{}) error {
tfeClient := meta.(*tfe.Client)
// Get the organization name.
organization := d.Get("organization").(string)
log.Printf("[DEBUG] Delete token from organization: %s", organization)
err := tfeClient.OrganizationTokens.Delete(ctx, organization)
if err != nil {
if err == tfe.ErrResourceNotFound {
return nil
}
return fmt.Errorf("Error deleting token from organization %s: %v", d.Id(), err)
}
return nil
}
func resourceTFEOrganizationTokenImporter(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
// Set the organization field.
d.Set("organization", d.Id())
return []*schema.ResourceData{d}, nil
}