|
| 1 | +package resources |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/hashicorp/terraform-plugin-framework/path" |
| 8 | + "github.com/hashicorp/terraform-plugin-framework/resource" |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 14 | + "github.com/qualitorque/terraform-provider-torque/client" |
| 15 | +) |
| 16 | + |
| 17 | +// Ensure provider defined types fully satisfy framework interfaces. |
| 18 | +var _ resource.Resource = &TorqueServiceNowApprovalChannelResource{} |
| 19 | +var _ resource.ResourceWithImportState = &TorqueServiceNowApprovalChannelResource{} |
| 20 | + |
| 21 | +func NewTorqueServiceNowApprovalChannelResource() resource.Resource { |
| 22 | + return &TorqueServiceNowApprovalChannelResource{} |
| 23 | +} |
| 24 | + |
| 25 | +// TorqueServiceNowApprovalChannelResource defines the resource implementation. |
| 26 | +type TorqueServiceNowApprovalChannelResource struct { |
| 27 | + client *client.Client |
| 28 | +} |
| 29 | + |
| 30 | +// TorqueServiceNowApprovalChannelResourceModel describes the resource data model. |
| 31 | +type TorqueServiceNowApprovalChannelResourceModel struct { |
| 32 | + Name types.String `tfsdk:"name"` |
| 33 | + Description types.String `tfsdk:"description"` |
| 34 | + Approver types.String `tfsdk:"approver"` |
| 35 | + BaseUrl types.String `tfsdk:"base_url"` |
| 36 | + UserName types.String `tfsdk:"user_name"` |
| 37 | + Password types.String `tfsdk:"password"` |
| 38 | + Headers types.String `tfsdk:"headers"` |
| 39 | +} |
| 40 | + |
| 41 | +const ( |
| 42 | + servicenow_approval_channel_type = "ServiceNow" |
| 43 | +) |
| 44 | + |
| 45 | +func (r *TorqueServiceNowApprovalChannelResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { |
| 46 | + resp.TypeName = "torque_servicenow_approval_channel" |
| 47 | +} |
| 48 | + |
| 49 | +func (r *TorqueServiceNowApprovalChannelResource) 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 ServiceNow 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 | + "base_url": schema.StringAttribute{ |
| 71 | + MarkdownDescription: "ServiceNow Instance Base URL", |
| 72 | + Optional: false, |
| 73 | + Computed: false, |
| 74 | + Required: true, |
| 75 | + }, |
| 76 | + "user_name": schema.StringAttribute{ |
| 77 | + MarkdownDescription: "ServiceNow Username", |
| 78 | + Optional: false, |
| 79 | + Computed: false, |
| 80 | + Required: true, |
| 81 | + }, |
| 82 | + "password": schema.StringAttribute{ |
| 83 | + MarkdownDescription: "ServiceNow Password", |
| 84 | + Optional: false, |
| 85 | + Computed: false, |
| 86 | + Required: true, |
| 87 | + Sensitive: true, |
| 88 | + }, |
| 89 | + "headers": schema.StringAttribute{ |
| 90 | + MarkdownDescription: "Custom Headers (JSON) - JSON formatted string that represents the custom headers, for example {header:'val'}", |
| 91 | + Optional: true, |
| 92 | + Computed: false, |
| 93 | + Required: false, |
| 94 | + }, |
| 95 | + "approver": schema.StringAttribute{ |
| 96 | + MarkdownDescription: "ServiceNow Approver", |
| 97 | + Optional: false, |
| 98 | + Computed: false, |
| 99 | + Required: true, |
| 100 | + }, |
| 101 | + }, |
| 102 | + } |
| 103 | +} |
| 104 | +func (r *TorqueServiceNowApprovalChannelResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) { |
| 105 | + // Prevent panic if the provider has not been configured. |
| 106 | + if req.ProviderData == nil { |
| 107 | + return |
| 108 | + } |
| 109 | + |
| 110 | + client, ok := req.ProviderData.(*client.Client) |
| 111 | + |
| 112 | + if !ok { |
| 113 | + resp.Diagnostics.AddError( |
| 114 | + "Unexpected Resource Configure Type", |
| 115 | + fmt.Sprintf("Expected *http.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData), |
| 116 | + ) |
| 117 | + |
| 118 | + return |
| 119 | + } |
| 120 | + |
| 121 | + r.client = client |
| 122 | +} |
| 123 | + |
| 124 | +func (r *TorqueServiceNowApprovalChannelResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { |
| 125 | + var data TorqueServiceNowApprovalChannelResourceModel |
| 126 | + var details client.ApprovalChannelDetails |
| 127 | + details.Approver = &client.Approver{} |
| 128 | + resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) |
| 129 | + |
| 130 | + if resp.Diagnostics.HasError() { |
| 131 | + return |
| 132 | + } |
| 133 | + |
| 134 | + details.Approver.UserEmail = data.Approver.ValueString() |
| 135 | + details.Type = servicenow_approval_channel_type |
| 136 | + details.BaseUrl = data.BaseUrl.ValueStringPointer() |
| 137 | + details.UserName = data.UserName.ValueStringPointer() |
| 138 | + details.Password = data.Password.ValueStringPointer() |
| 139 | + details.Headers = data.Headers.ValueStringPointer() |
| 140 | + err := r.client.CreateApprovalChannel(data.Name.ValueString(), data.Description.ValueString(), details) |
| 141 | + if err != nil { |
| 142 | + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to create Approval Channel, got error: %s", err)) |
| 143 | + return |
| 144 | + } |
| 145 | + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) |
| 146 | +} |
| 147 | + |
| 148 | +func (r *TorqueServiceNowApprovalChannelResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { |
| 149 | + var data TorqueServiceNowApprovalChannelResourceModel |
| 150 | + |
| 151 | + diags := req.State.Get(ctx, &data) |
| 152 | + resp.Diagnostics.Append(diags...) |
| 153 | + if resp.Diagnostics.HasError() { |
| 154 | + return |
| 155 | + } |
| 156 | + approval_channel, err := r.client.GetApprovalChannel(data.Name.ValueString()) |
| 157 | + if err != nil { |
| 158 | + resp.Diagnostics.AddError( |
| 159 | + "Error Reading Approval Channel details", |
| 160 | + "Could not read Approval Channel "+data.Name.ValueString()+": "+err.Error(), |
| 161 | + ) |
| 162 | + return |
| 163 | + } |
| 164 | + data.Name = types.StringValue(approval_channel.Name) |
| 165 | + data.Description = types.StringValue(approval_channel.Description) |
| 166 | + data.BaseUrl = types.StringPointerValue(approval_channel.Details.BaseUrl) |
| 167 | + |
| 168 | + data.Headers = types.StringPointerValue(approval_channel.Details.Headers) |
| 169 | + data.Approver = types.StringPointerValue(&approval_channel.Details.Approver.UserEmail) |
| 170 | + |
| 171 | + diags = resp.State.Set(ctx, &data) |
| 172 | + resp.Diagnostics.Append(diags...) |
| 173 | + if resp.Diagnostics.HasError() { |
| 174 | + return |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +func (r *TorqueServiceNowApprovalChannelResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { |
| 179 | + var data TorqueServiceNowApprovalChannelResourceModel |
| 180 | + var details client.ApprovalChannelDetails |
| 181 | + details.Approver = &client.Approver{} |
| 182 | + |
| 183 | + resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) |
| 184 | + |
| 185 | + details.Approver.UserEmail = data.Approver.ValueString() |
| 186 | + details.Type = servicenow_approval_channel_type |
| 187 | + details.BaseUrl = data.BaseUrl.ValueStringPointer() |
| 188 | + details.UserName = data.UserName.ValueStringPointer() |
| 189 | + details.Password = data.Password.ValueStringPointer() |
| 190 | + details.Headers = data.Headers.ValueStringPointer() |
| 191 | + err := r.client.UpdateApprovalChannel(data.Name.ValueString(), data.Description.ValueString(), details) |
| 192 | + if err != nil { |
| 193 | + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to update Approval Channel, got error: %s", err)) |
| 194 | + return |
| 195 | + } |
| 196 | + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) |
| 197 | +} |
| 198 | + |
| 199 | +func (r *TorqueServiceNowApprovalChannelResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { |
| 200 | + var data TorqueServiceNowApprovalChannelResourceModel |
| 201 | + |
| 202 | + // Read Terraform prior state data into the model. |
| 203 | + resp.Diagnostics.Append(req.State.Get(ctx, &data)...) |
| 204 | + |
| 205 | + if resp.Diagnostics.HasError() { |
| 206 | + return |
| 207 | + } |
| 208 | + |
| 209 | + err := r.client.DeleteApprovalChannel(data.Name.ValueString()) |
| 210 | + if err != nil { |
| 211 | + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to delete Approval Channel, got error: %s", err)) |
| 212 | + return |
| 213 | + } |
| 214 | + |
| 215 | +} |
| 216 | + |
| 217 | +func (r *TorqueServiceNowApprovalChannelResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { |
| 218 | + resource.ImportStatePassthroughID(ctx, path.Root("name"), req, resp) |
| 219 | +} |
0 commit comments