generated from hashicorp/terraform-provider-scaffolding-framework
/
resource_agent_queue.go
177 lines (149 loc) · 5.7 KB
/
resource_agent_queue.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
package pipelines
import (
"context"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/int64planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/scordonnier/terraform-provider-azuredevops/internal/clients"
"github.com/scordonnier/terraform-provider-azuredevops/internal/clients/pipelines"
"github.com/scordonnier/terraform-provider-azuredevops/internal/utils"
"github.com/scordonnier/terraform-provider-azuredevops/internal/validators"
"strconv"
)
var _ resource.Resource = &AgentQueueResource{}
func NewAgentQueueResource() resource.Resource {
return &AgentQueueResource{}
}
type AgentQueueResource struct {
client *pipelines.Client
}
type AgentQueueResourceModel struct {
AgentPoolId int `tfsdk:"agent_pool_id"`
GrantAllPipelines bool `tfsdk:"grant_all_pipelines"`
Id types.Int64 `tfsdk:"id"`
Name string `tfsdk:"name"`
ProjectId string `tfsdk:"project_id"`
}
func (r *AgentQueueResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_agent_queue"
}
func (r *AgentQueueResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "Manage agent queues within an Azure DevOps project.",
Attributes: map[string]schema.Attribute{
"agent_pool_id": schema.Int64Attribute{
MarkdownDescription: "The ID of the agent pool.",
Required: true,
PlanModifiers: []planmodifier.Int64{
int64planmodifier.RequiresReplace(),
},
},
"grant_all_pipelines": schema.BoolAttribute{
MarkdownDescription: "Set to true to grant access to all pipelines in the project.",
Required: true,
},
"id": schema.Int64Attribute{
MarkdownDescription: "The ID of the queue.",
Computed: true,
PlanModifiers: []planmodifier.Int64{
int64planmodifier.UseStateForUnknown(),
},
},
"name": schema.StringAttribute{
MarkdownDescription: "The name of the queue.",
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
Validators: []validator.String{
validators.StringNotEmpty(),
},
},
"project_id": schema.StringAttribute{
Required: true,
MarkdownDescription: "The ID of the project.",
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
Validators: []validator.String{
validators.UUID(),
},
},
},
}
}
func (r *AgentQueueResource) Configure(_ context.Context, req resource.ConfigureRequest, _ *resource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
r.client = req.ProviderData.(*clients.AzureDevOpsClient).PipelinesClient
}
func (r *AgentQueueResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var model *AgentQueueResourceModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &model)...)
if resp.Diagnostics.HasError() {
return
}
queue, err := r.client.CreateAgentQueue(ctx, model.ProjectId, model.AgentPoolId, model.Name, model.GrantAllPipelines)
if err != nil {
resp.Diagnostics.AddError("Unable to create queue", err.Error())
return
}
model.Id = types.Int64Value(int64(*queue.Id))
resp.Diagnostics.Append(resp.State.Set(ctx, &model)...)
}
func (r *AgentQueueResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var model *AgentQueueResourceModel
resp.Diagnostics.Append(req.State.Get(ctx, &model)...)
if resp.Diagnostics.HasError() {
return
}
_, err := r.client.GetAgentQueue(ctx, model.ProjectId, int(model.Id.ValueInt64()))
if err != nil {
if utils.ResponseWasNotFound(err) {
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("Unable to retrieve queue", err.Error())
return
}
granted, err := r.client.GetPipelinePermissions(ctx, model.ProjectId, pipelines.PipelinePermissionsResourceTypeQueue, strconv.Itoa(int(model.Id.ValueInt64())))
if err != nil {
resp.Diagnostics.AddError("Unable to retrieve grant status", err.Error())
return
}
if granted.AllPipelines != nil {
model.GrantAllPipelines = *granted.AllPipelines.Authorized
} else {
model.GrantAllPipelines = false
}
resp.Diagnostics.Append(resp.State.Set(ctx, &model)...)
}
func (r *AgentQueueResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
var model *AgentQueueResourceModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &model)...)
if resp.Diagnostics.HasError() {
return
}
_, err := r.client.GrantAllPipelines(ctx, model.ProjectId, pipelines.PipelinePermissionsResourceTypeQueue, strconv.Itoa(int(model.Id.ValueInt64())), model.GrantAllPipelines)
if err != nil {
resp.Diagnostics.AddError("Unable to update queue", err.Error())
return
}
resp.Diagnostics.Append(resp.State.Set(ctx, &model)...)
}
func (r *AgentQueueResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var model *AgentQueueResourceModel
resp.Diagnostics.Append(req.State.Get(ctx, &model)...)
if resp.Diagnostics.HasError() {
return
}
err := r.client.DeleteAgentQueue(ctx, model.ProjectId, int(model.Id.ValueInt64()))
if err != nil {
resp.Diagnostics.AddError("Unable to delete queue", err.Error())
}
}