-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathresource_team.go
145 lines (130 loc) · 3.4 KB
/
resource_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
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
package awx
import (
"fmt"
"strconv"
"time"
"github.com/hashicorp/terraform/helper/schema"
awxgo "github.com/mauromedda/awx-go"
)
func resourceTeamObject() *schema.Resource {
return &schema.Resource{
Create: resourceTeamCreate,
Read: resourceTeamRead,
Delete: resourceTeamDelete,
Update: resourceTeamUpdate,
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "The name of the Team.",
},
"description": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "An optional team decription.",
},
"organization_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "The ID of the organization.",
},
},
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(1 * time.Minute),
Update: schema.DefaultTimeout(1 * time.Minute),
Delete: schema.DefaultTimeout(5 * time.Minute),
},
}
}
func resourceTeamCreate(d *schema.ResourceData, m interface{}) error {
awx := m.(*awxgo.AWX)
awxService := awx.TeamService
_, res, err := awxService.ListTeams(map[string]string{
"name": d.Get("name").(string)})
if err != nil {
return err
}
if len(res.Results) >= 1 {
return fmt.Errorf("Team with name %s already exists",
d.Get("name").(string))
}
result, err := awxService.CreateTeam(map[string]interface{}{
"name": d.Get("name").(string),
"description": d.Get("description").(string),
"organization": d.Get("organization_id").(string),
}, map[string]string{})
if err != nil {
return err
}
d.SetId(strconv.Itoa(result.ID))
return resourceTeamRead(d, m)
}
func resourceTeamUpdate(d *schema.ResourceData, m interface{}) error {
awx := m.(*awxgo.AWX)
awxService := awx.TeamService
_, res, err := awxService.ListTeams(map[string]string{
"id": d.Id()},
)
if err != nil {
return err
}
if len(res.Results) == 0 {
return fmt.Errorf("Team with name %s doesn't exists",
d.Get("name").(string))
}
id, err := strconv.Atoi(d.Id())
if err != nil {
return err
}
_, err = awxService.UpdateTeam(id, map[string]interface{}{
"name": d.Get("name").(string),
"description": d.Get("description").(string),
"organization": d.Get("organization_id").(string),
}, map[string]string{})
if err != nil {
return err
}
return resourceTeamRead(d, m)
}
func resourceTeamRead(d *schema.ResourceData, m interface{}) error {
awx := m.(*awxgo.AWX)
awxService := awx.TeamService
_, res, err := awxService.ListTeams(map[string]string{
"name": d.Get("name").(string)})
if err != nil {
return err
}
if len(res.Results) == 0 {
return nil
}
d = setTeamResourceData(d, res.Results[0])
return nil
}
func resourceTeamDelete(d *schema.ResourceData, m interface{}) error {
awx := m.(*awxgo.AWX)
awxService := awx.TeamService
id, err := strconv.Atoi(d.Id())
_, res, err := awxService.ListTeams(map[string]string{
"name": d.Get("name").(string)})
if len(res.Results) == 0 {
d.SetId("")
return nil
}
if err != nil {
return err
}
if _, err = awxService.DeleteTeam(id); err != nil {
return err
}
d.SetId("")
return nil
}
func setTeamResourceData(d *schema.ResourceData, r *awxgo.Team) *schema.ResourceData {
d.Set("name", r.Name)
d.Set("description", r.Description)
d.Set("organization", r.Organization)
return d
}