|
| 1 | +package flagsmith |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "github.com/Flagsmith/flagsmith-go-api-client" |
| 7 | + "github.com/hashicorp/terraform-plugin-framework/datasource" |
| 8 | + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" |
| 9 | +) |
| 10 | + |
| 11 | +// Ensure provider defined types fully satisfy framework interfaces |
| 12 | +var _ datasource.DataSource = &organisationDataResource{} |
| 13 | + |
| 14 | +func newOrganisationDataResource() datasource.DataSource { |
| 15 | + return &organisationDataResource{} |
| 16 | +} |
| 17 | + |
| 18 | +type organisationDataResource struct { |
| 19 | + client *flagsmithapi.Client |
| 20 | +} |
| 21 | + |
| 22 | +func (o *organisationDataResource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { |
| 23 | + resp.TypeName = req.ProviderTypeName + "_organisation" |
| 24 | +} |
| 25 | + |
| 26 | +func (o *organisationDataResource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { |
| 27 | + // Prevent panic if the provider has not been configured. |
| 28 | + if req.ProviderData == nil { |
| 29 | + return |
| 30 | + } |
| 31 | + |
| 32 | + client, ok := req.ProviderData.(*flagsmithapi.Client) |
| 33 | + if !ok { |
| 34 | + resp.Diagnostics.AddError( |
| 35 | + "Unexpected Resource Configure Type", |
| 36 | + fmt.Sprintf("Expected *flagsmithapi.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData), |
| 37 | + ) |
| 38 | + return |
| 39 | + } |
| 40 | + |
| 41 | + o.client = client |
| 42 | +} |
| 43 | +func (o *organisationDataResource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { |
| 44 | + resp.Schema = schema.Schema{ |
| 45 | + // This description is used by the documentation generator and the language server. |
| 46 | + MarkdownDescription: "Flagsmith Organisation", |
| 47 | + |
| 48 | + Attributes: map[string]schema.Attribute{ |
| 49 | + "uuid": schema.StringAttribute{ |
| 50 | + Required: true, |
| 51 | + MarkdownDescription: "UUID of the organisation", |
| 52 | + }, |
| 53 | + "id": schema.Int64Attribute{ |
| 54 | + Computed: true, |
| 55 | + MarkdownDescription: "ID of the organisation", |
| 56 | + }, |
| 57 | + "name": schema.StringAttribute{ |
| 58 | + Computed: true, |
| 59 | + MarkdownDescription: "Name of the organisation", |
| 60 | + }, |
| 61 | + "force_2fa": schema.BoolAttribute{ |
| 62 | + Computed: true, |
| 63 | + MarkdownDescription: "If true, signup will require 2FA", |
| 64 | + }, |
| 65 | + "persist_trait_data": schema.BoolAttribute{ |
| 66 | + Computed: true, |
| 67 | + MarkdownDescription: "If false, trait data for this organisation identities will not stored", |
| 68 | + }, |
| 69 | + "restrict_project_create_to_admin": schema.BoolAttribute{ |
| 70 | + Computed: true, |
| 71 | + MarkdownDescription: "If true, only organisation admin can create projects", |
| 72 | + }, |
| 73 | + }, |
| 74 | + } |
| 75 | +} |
| 76 | +func (o *organisationDataResource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { |
| 77 | + var data OrganisationResourceData |
| 78 | + diags := req.Config.Get(ctx, &data) |
| 79 | + resp.Diagnostics.Append(diags...) |
| 80 | + |
| 81 | + // Early return if the state is wrong |
| 82 | + if diags.HasError() { |
| 83 | + return |
| 84 | + } |
| 85 | + |
| 86 | + organisation, err := o.client.GetOrganisationByUUID(data.UUID.ValueString()) |
| 87 | + if err != nil { |
| 88 | + panic(err) |
| 89 | + |
| 90 | + } |
| 91 | + resourceData := MakeOrganisationResourceDataFromClientOrganisation(organisation) |
| 92 | + |
| 93 | + diags = resp.State.Set(ctx, &resourceData) |
| 94 | + resp.Diagnostics.Append(diags...) |
| 95 | + |
| 96 | +} |
0 commit comments