|
| 1 | +package resource |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "terraform-provider-plural/internal/client" |
| 8 | + "terraform-provider-plural/internal/common" |
| 9 | + "terraform-provider-plural/internal/model" |
| 10 | + |
| 11 | + "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/path" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/resource" |
| 14 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema" |
| 15 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" |
| 16 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault" |
| 17 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" |
| 18 | + "github.com/hashicorp/terraform-plugin-framework/schema/validator" |
| 19 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 20 | + gqlclient "github.com/pluralsh/console/go/client" |
| 21 | + "github.com/pluralsh/polly/algorithms" |
| 22 | +) |
| 23 | + |
| 24 | +var _ resource.Resource = &OIDCProviderResource{} |
| 25 | +var _ resource.ResourceWithImportState = &OIDCProviderResource{} |
| 26 | + |
| 27 | +func NewOIDCProviderResourceResource() resource.Resource { |
| 28 | + return &OIDCProviderResource{} |
| 29 | +} |
| 30 | + |
| 31 | +// OIDCProviderResource defines the OIDC provider resource implementation. |
| 32 | +type OIDCProviderResource struct { |
| 33 | + client *client.Client |
| 34 | +} |
| 35 | + |
| 36 | +func (r *OIDCProviderResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { |
| 37 | + resp.TypeName = req.ProviderTypeName + "_oidc_provider" |
| 38 | +} |
| 39 | + |
| 40 | +func (r *OIDCProviderResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { |
| 41 | + resp.Schema = schema.Schema{ |
| 42 | + Attributes: map[string]schema.Attribute{ |
| 43 | + "id": schema.StringAttribute{ |
| 44 | + Description: "Internal identifier of this OIDC provider.", |
| 45 | + MarkdownDescription: "Internal identifier of this OIDC provider.", |
| 46 | + Computed: true, |
| 47 | + PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()}, |
| 48 | + }, |
| 49 | + "name": schema.StringAttribute{ |
| 50 | + Description: "Human-readable name of this OIDC provider.", |
| 51 | + MarkdownDescription: "Human-readable name of this OIDC provider.", |
| 52 | + Required: true, |
| 53 | + PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()}, |
| 54 | + }, |
| 55 | + "type": schema.StringAttribute{ |
| 56 | + Required: true, |
| 57 | + PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()}, |
| 58 | + Validators: []validator.String{ |
| 59 | + stringvalidator.OneOf( |
| 60 | + algorithms.Map(gqlclient.AllOidcProviderType, |
| 61 | + func(t gqlclient.OidcProviderType) string { return string(t) })...), |
| 62 | + }, |
| 63 | + }, |
| 64 | + "description": schema.StringAttribute{ |
| 65 | + Description: "Description of this OIDC provider.", |
| 66 | + MarkdownDescription: "Description of this OIDC provider.", |
| 67 | + Optional: true, |
| 68 | + Validators: []validator.String{stringvalidator.LengthAtLeast(1)}, |
| 69 | + }, |
| 70 | + "client_id": schema.StringAttribute{ |
| 71 | + Computed: true, |
| 72 | + Sensitive: true, |
| 73 | + PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()}, |
| 74 | + }, |
| 75 | + "client_secret": schema.StringAttribute{ |
| 76 | + Computed: true, |
| 77 | + Sensitive: true, |
| 78 | + PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()}, |
| 79 | + }, |
| 80 | + "auth_method": schema.StringAttribute{ |
| 81 | + Optional: true, |
| 82 | + Computed: true, |
| 83 | + Default: stringdefault.StaticString(string(gqlclient.OidcAuthMethodBasic)), |
| 84 | + PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()}, |
| 85 | + Validators: []validator.String{ |
| 86 | + stringvalidator.OneOf( |
| 87 | + algorithms.Map(gqlclient.AllOidcAuthMethod, |
| 88 | + func(t gqlclient.OidcAuthMethod) string { return string(t) })...), |
| 89 | + }, |
| 90 | + }, |
| 91 | + "redirect_uris": schema.SetAttribute{ |
| 92 | + Optional: true, |
| 93 | + ElementType: types.StringType, |
| 94 | + }, |
| 95 | + }, |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +func (r *OIDCProviderResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) { |
| 100 | + if req.ProviderData == nil { |
| 101 | + return |
| 102 | + } |
| 103 | + |
| 104 | + data, ok := req.ProviderData.(*common.ProviderData) |
| 105 | + if !ok { |
| 106 | + resp.Diagnostics.AddError( |
| 107 | + "Unexpected OIDC Provider Resource Configure Type", |
| 108 | + fmt.Sprintf("Expected *common.ProviderData, got: %T. Please report this issue to the provider developers.", req.ProviderData), |
| 109 | + ) |
| 110 | + |
| 111 | + return |
| 112 | + } |
| 113 | + |
| 114 | + r.client = data.Client |
| 115 | +} |
| 116 | + |
| 117 | +func (r *OIDCProviderResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { |
| 118 | + data := new(model.OIDCProvider) |
| 119 | + resp.Diagnostics.Append(req.Plan.Get(ctx, data)...) |
| 120 | + if resp.Diagnostics.HasError() { |
| 121 | + return |
| 122 | + } |
| 123 | + |
| 124 | + result, err := r.client.CreateOIDCProvider(ctx, data.TypeAttribute(), data.Attributes(ctx, resp.Diagnostics)) |
| 125 | + if err != nil { |
| 126 | + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to create OIDC provider, got error: %s", err)) |
| 127 | + return |
| 128 | + } |
| 129 | + |
| 130 | + data.From(result.CreateOidcProvider, ctx, resp.Diagnostics) |
| 131 | + resp.Diagnostics.Append(resp.State.Set(ctx, data)...) |
| 132 | +} |
| 133 | + |
| 134 | +func (r *OIDCProviderResource) Read(_ context.Context, _ resource.ReadRequest, _ *resource.ReadResponse) { |
| 135 | + // Ignore. |
| 136 | +} |
| 137 | + |
| 138 | +func (r *OIDCProviderResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { |
| 139 | + data := new(model.OIDCProvider) |
| 140 | + resp.Diagnostics.Append(req.Plan.Get(ctx, data)...) |
| 141 | + if resp.Diagnostics.HasError() { |
| 142 | + return |
| 143 | + } |
| 144 | + |
| 145 | + result, err := r.client.UpdateOIDCProvider(ctx, data.ID.ValueString(), data.TypeAttribute(), data.Attributes(ctx, resp.Diagnostics)) |
| 146 | + if err != nil { |
| 147 | + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to update OIDC provider, got error: %s", err)) |
| 148 | + return |
| 149 | + } |
| 150 | + |
| 151 | + data.From(result.UpdateOidcProvider, ctx, resp.Diagnostics) |
| 152 | + resp.Diagnostics.Append(resp.State.Set(ctx, data)...) |
| 153 | +} |
| 154 | + |
| 155 | +func (r *OIDCProviderResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { |
| 156 | + data := new(model.OIDCProvider) |
| 157 | + resp.Diagnostics.Append(req.State.Get(ctx, data)...) |
| 158 | + if resp.Diagnostics.HasError() { |
| 159 | + return |
| 160 | + } |
| 161 | + |
| 162 | + _, err := r.client.DeleteOIDCProvider(ctx, data.ID.ValueString(), data.TypeAttribute()) |
| 163 | + if err != nil { |
| 164 | + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to delete OIDC provider, got error: %s", err)) |
| 165 | + return |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +func (r *OIDCProviderResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { |
| 170 | + resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp) |
| 171 | +} |
0 commit comments