|
| 1 | +package resources |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-framework-validators/listvalidator" |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/path" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/resource" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault" |
| 14 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" |
| 15 | + "github.com/hashicorp/terraform-plugin-framework/schema/validator" |
| 16 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 17 | + "github.com/qualitorque/terraform-provider-torque/client" |
| 18 | +) |
| 19 | + |
| 20 | +// Ensure provider defined types fully satisfy framework interfaces. |
| 21 | +var _ resource.Resource = &TorqueTeamsApprovalChannelResource{} |
| 22 | +var _ resource.ResourceWithImportState = &TorqueTeamsApprovalChannelResource{} |
| 23 | + |
| 24 | +func NewTorqueTeamsApprovalChannelResource() resource.Resource { |
| 25 | + return &TorqueTeamsApprovalChannelResource{} |
| 26 | +} |
| 27 | + |
| 28 | +// TorqueTeamsApprovalChannelResource defines the resource implementation. |
| 29 | +type TorqueTeamsApprovalChannelResource struct { |
| 30 | + client *client.Client |
| 31 | +} |
| 32 | + |
| 33 | +// TorqueTeamsApprovalChannelResourceModel describes the resource data model. |
| 34 | +type TorqueTeamsApprovalChannelResourceModel struct { |
| 35 | + Name types.String `tfsdk:"name"` |
| 36 | + Description types.String `tfsdk:"description"` |
| 37 | + Approvers types.List `tfsdk:"approvers"` |
| 38 | + WebhookAddress types.String `tfsdk:"webhook_address"` |
| 39 | +} |
| 40 | + |
| 41 | +const ( |
| 42 | + teams_approval_channel_type = "Teams" |
| 43 | +) |
| 44 | + |
| 45 | +func (r *TorqueTeamsApprovalChannelResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { |
| 46 | + resp.TypeName = "torque_teams_approval_channel" |
| 47 | +} |
| 48 | + |
| 49 | +func (r *TorqueTeamsApprovalChannelResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) { |
| 50 | + resp.Schema = schema.Schema{ |
| 51 | + // This description is used by the documentation generator and the language server. |
| 52 | + MarkdownDescription: "Creates a new MS Teams approval channel.", |
| 53 | + Attributes: map[string]schema.Attribute{ |
| 54 | + "name": schema.StringAttribute{ |
| 55 | + MarkdownDescription: "Name of the approval channel.", |
| 56 | + Optional: false, |
| 57 | + Computed: false, |
| 58 | + Required: true, |
| 59 | + PlanModifiers: []planmodifier.String{ |
| 60 | + stringplanmodifier.RequiresReplace(), |
| 61 | + }, |
| 62 | + }, |
| 63 | + "description": schema.StringAttribute{ |
| 64 | + MarkdownDescription: "Description of the approval channel", |
| 65 | + Optional: true, |
| 66 | + Computed: true, |
| 67 | + Required: false, |
| 68 | + Default: stringdefault.StaticString(""), |
| 69 | + }, |
| 70 | + "webhook_address": schema.StringAttribute{ |
| 71 | + MarkdownDescription: "MS Teams Webhook Address", |
| 72 | + Optional: false, |
| 73 | + Computed: false, |
| 74 | + Required: true, |
| 75 | + }, |
| 76 | + "approvers": schema.ListAttribute{ |
| 77 | + Description: "List of existing emails of users that will be the approvers of this approval channel", |
| 78 | + Required: true, |
| 79 | + Optional: false, |
| 80 | + ElementType: types.StringType, |
| 81 | + Validators: []validator.List{ |
| 82 | + listvalidator.SizeAtLeast(1), // Ensure the list has at least one entry if required |
| 83 | + }, |
| 84 | + }, |
| 85 | + }, |
| 86 | + } |
| 87 | +} |
| 88 | +func (r *TorqueTeamsApprovalChannelResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) { |
| 89 | + // Prevent panic if the provider has not been configured. |
| 90 | + if req.ProviderData == nil { |
| 91 | + return |
| 92 | + } |
| 93 | + |
| 94 | + client, ok := req.ProviderData.(*client.Client) |
| 95 | + |
| 96 | + if !ok { |
| 97 | + resp.Diagnostics.AddError( |
| 98 | + "Unexpected Resource Configure Type", |
| 99 | + fmt.Sprintf("Expected *http.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData), |
| 100 | + ) |
| 101 | + |
| 102 | + return |
| 103 | + } |
| 104 | + |
| 105 | + r.client = client |
| 106 | +} |
| 107 | + |
| 108 | +func (r *TorqueTeamsApprovalChannelResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { |
| 109 | + var data TorqueTeamsApprovalChannelResourceModel |
| 110 | + var details client.ApprovalChannelDetails |
| 111 | + var approvers []client.Approver |
| 112 | + resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) |
| 113 | + |
| 114 | + if resp.Diagnostics.HasError() { |
| 115 | + return |
| 116 | + } |
| 117 | + |
| 118 | + for _, approver := range data.Approvers.Elements() { |
| 119 | + approvers = append(approvers, client.Approver{ |
| 120 | + UserEmail: strings.Replace(approver.String(), "\"", "", -1), |
| 121 | + }) |
| 122 | + } |
| 123 | + details.Approvers = approvers |
| 124 | + details.Type = teams_approval_channel_type |
| 125 | + details.WebhookAddress = data.WebhookAddress.ValueStringPointer() |
| 126 | + err := r.client.CreateApprovalChannel(data.Name.ValueString(), data.Description.ValueString(), details) |
| 127 | + if err != nil { |
| 128 | + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to create Approval Channel, got error: %s", err)) |
| 129 | + return |
| 130 | + } |
| 131 | + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) |
| 132 | +} |
| 133 | + |
| 134 | +func (r *TorqueTeamsApprovalChannelResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { |
| 135 | + var data TorqueTeamsApprovalChannelResourceModel |
| 136 | + approvers := []string{} |
| 137 | + diags := req.State.Get(ctx, &data) |
| 138 | + resp.Diagnostics.Append(diags...) |
| 139 | + if resp.Diagnostics.HasError() { |
| 140 | + return |
| 141 | + } |
| 142 | + approval_channel, err := r.client.GetApprovalChannel(data.Name.ValueString()) |
| 143 | + if err != nil { |
| 144 | + resp.Diagnostics.AddError( |
| 145 | + "Error Reading Approval Channel details", |
| 146 | + "Could not read Approval Channel "+data.Name.ValueString()+": "+err.Error(), |
| 147 | + ) |
| 148 | + return |
| 149 | + } |
| 150 | + data.Name = types.StringValue(approval_channel.Name) |
| 151 | + data.Description = types.StringValue(approval_channel.Description) |
| 152 | + data.WebhookAddress = types.StringPointerValue(approval_channel.Details.WebhookAddress) |
| 153 | + for _, approver := range approval_channel.Details.Approvers { |
| 154 | + approvers = append(approvers, approver.UserEmail) |
| 155 | + } |
| 156 | + data.Approvers, _ = types.ListValueFrom(ctx, types.StringType, approvers) |
| 157 | + |
| 158 | + diags = resp.State.Set(ctx, &data) |
| 159 | + resp.Diagnostics.Append(diags...) |
| 160 | + if resp.Diagnostics.HasError() { |
| 161 | + return |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +func (r *TorqueTeamsApprovalChannelResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { |
| 166 | + var data TorqueTeamsApprovalChannelResourceModel |
| 167 | + var details client.ApprovalChannelDetails |
| 168 | + var approvers []client.Approver |
| 169 | + |
| 170 | + resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) |
| 171 | + |
| 172 | + for _, approver := range data.Approvers.Elements() { |
| 173 | + approvers = append(approvers, client.Approver{ |
| 174 | + UserEmail: strings.Replace(approver.String(), "\"", "", -1), |
| 175 | + }) |
| 176 | + } |
| 177 | + details.Approvers = approvers |
| 178 | + details.Type = teams_approval_channel_type |
| 179 | + details.WebhookAddress = data.WebhookAddress.ValueStringPointer() |
| 180 | + err := r.client.UpdateApprovalChannel(data.Name.ValueString(), data.Description.ValueString(), details) |
| 181 | + if err != nil { |
| 182 | + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to update Input Source, got error: %s", err)) |
| 183 | + return |
| 184 | + } |
| 185 | + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) |
| 186 | +} |
| 187 | + |
| 188 | +func (r *TorqueTeamsApprovalChannelResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { |
| 189 | + var data TorqueTeamsApprovalChannelResourceModel |
| 190 | + |
| 191 | + // Read Terraform prior state data into the model. |
| 192 | + resp.Diagnostics.Append(req.State.Get(ctx, &data)...) |
| 193 | + |
| 194 | + if resp.Diagnostics.HasError() { |
| 195 | + return |
| 196 | + } |
| 197 | + |
| 198 | + err := r.client.DeleteApprovalChannel(data.Name.ValueString()) |
| 199 | + if err != nil { |
| 200 | + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to delete Approval Channel, got error: %s", err)) |
| 201 | + return |
| 202 | + } |
| 203 | + |
| 204 | +} |
| 205 | + |
| 206 | +func (r *TorqueTeamsApprovalChannelResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { |
| 207 | + resource.ImportStatePassthroughID(ctx, path.Root("name"), req, resp) |
| 208 | +} |
0 commit comments