This repository has been archived by the owner on Mar 5, 2024. It is now read-only.
generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 1
/
resource_team.go
201 lines (174 loc) · 4.38 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
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
package provider
import (
"context"
"github.com/PsypherPunk/terraform-provider-ctfd/internal/api"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"strconv"
)
func resourceTeamCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*api.Client)
var diags diag.Diagnostics
team := api.NewTeam{
Name: d.Get("name").(string),
Email: d.Get("email").(string),
Password: d.Get("password").(string),
Website: d.Get("website").(string),
Affiliation: d.Get("affiliation").(string),
Country: d.Get("country").(string),
Hidden: d.Get("hidden").(bool),
Banned: d.Get("banned").(bool),
}
newTeam, err := client.CreateTeam(team)
if err != nil {
return diag.FromErr(err)
}
d.SetId(strconv.Itoa(int(newTeam.Id)))
err = d.Set("members", newTeam.Members)
if err != nil {
return nil
}
err = d.Set("fields", newTeam.Fields)
if err != nil {
return nil
}
return diags
}
func resourceTeamRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*api.Client)
var diags diag.Diagnostics
id := d.Get("id").(string)
intId, err := strconv.Atoi(id)
if err != nil {
return nil
}
team, err := client.GetTeam(uint(intId))
if err != nil {
return diag.FromErr(err)
}
d.SetId(strconv.Itoa(int(team.Id)))
return diags
}
func resourceTeamUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*api.Client)
var diags diag.Diagnostics
id := d.Id()
intId, err := strconv.Atoi(id)
if err != nil {
return nil
}
team := new(api.NewTeam)
team.Password = d.Get("password").(string)
team.Name = d.Get("name").(string)
team.Email = d.Get("email").(string)
team.Website = d.Get("website").(string)
team.Affiliation = d.Get("affiliation").(string)
team.Country = d.Get("country").(string)
team.Hidden = d.Get("hidden").(bool)
team.Banned = d.Get("banned").(bool)
updatedTeam, err := client.UpdateTeam(uint(intId), *team)
if err != nil {
return diag.FromErr(err)
}
d.SetId(strconv.Itoa(int(updatedTeam.Id)))
return diags
}
func resourceTeamDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*api.Client)
id := d.Id()
var diags diag.Diagnostics
intId, err := strconv.Atoi(id)
if err != nil {
return nil
}
err = client.DeleteTeam(uint(intId))
if err != nil {
return diag.FromErr(err)
}
return diags
}
func resourceTeam() *schema.Resource {
return &schema.Resource{
Description: "Get details of a teams.",
CreateContext: resourceTeamCreate,
ReadContext: resourceTeamRead,
UpdateContext: resourceTeamUpdate,
DeleteContext: resourceTeamDelete,
Schema: map[string]*schema.Schema{
"id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"email": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"password": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"website": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"affiliation": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"country": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"hidden": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
},
"banned": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
},
"captain_id": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
Optional: true,
},
"bracket": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Optional: true,
},
"secret": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Optional: true,
},
"oauth_id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Optional: true,
},
"members": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeInt,
},
},
"created": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"fields": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
}