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_team.go
110 lines (89 loc) · 2.5 KB
/
resource_tfe_team.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 tfe
import (
"fmt"
"log"
"strings"
tfe "github.com/hashicorp/go-tfe"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceTFETeam() *schema.Resource {
return &schema.Resource{
Create: resourceTFETeamCreate,
Read: resourceTFETeamRead,
Delete: resourceTFETeamDelete,
Importer: &schema.ResourceImporter{
State: resourceTFETeamImporter,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"organization": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}
func resourceTFETeamCreate(d *schema.ResourceData, meta interface{}) error {
tfeClient := meta.(*tfe.Client)
// Get the name and organization.
name := d.Get("name").(string)
organization := d.Get("organization").(string)
// Create a new options struct.
options := tfe.TeamCreateOptions{
Name: tfe.String(name),
}
log.Printf("[DEBUG] Create team %s for organization: %s", name, organization)
team, err := tfeClient.Teams.Create(ctx, organization, options)
if err != nil {
return fmt.Errorf(
"Error creating team %s for organization %s: %v", name, organization, err)
}
d.SetId(team.ID)
return resourceTFETeamRead(d, meta)
}
func resourceTFETeamRead(d *schema.ResourceData, meta interface{}) error {
tfeClient := meta.(*tfe.Client)
log.Printf("[DEBUG] Read configuration of team: %s", d.Id())
team, err := tfeClient.Teams.Read(ctx, d.Id())
if err != nil {
if err == tfe.ErrResourceNotFound {
log.Printf("[DEBUG] Team %s does no longer exist", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("Error reading configuration of team %s: %v", d.Id(), err)
}
// Update the config.
d.Set("name", team.Name)
return nil
}
func resourceTFETeamDelete(d *schema.ResourceData, meta interface{}) error {
tfeClient := meta.(*tfe.Client)
log.Printf("[DEBUG] Delete team: %s", d.Id())
err := tfeClient.Teams.Delete(ctx, d.Id())
if err != nil {
if err == tfe.ErrResourceNotFound {
return nil
}
return fmt.Errorf("Error deleting team %s: %v", d.Id(), err)
}
return nil
}
func resourceTFETeamImporter(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
s := strings.SplitN(d.Id(), "/", 2)
if len(s) != 2 {
return nil, fmt.Errorf(
"invalid team import format: %s (expected <ORGANIZATION>/<TEAM ID>)",
d.Id(),
)
}
// Set the fields that are part of the import ID.
d.Set("organization", s[0])
d.SetId(s[1])
return []*schema.ResourceData{d}, nil
}