|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/meshcloud/terraform-provider-meshstack/client" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/datasource" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/path" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 13 | +) |
| 14 | + |
| 15 | +var ( |
| 16 | + _ datasource.DataSource = &tenantV4DataSource{} |
| 17 | + _ datasource.DataSourceWithConfigure = &tenantV4DataSource{} |
| 18 | +) |
| 19 | + |
| 20 | +func NewTenantV4DataSource() datasource.DataSource { |
| 21 | + return &tenantV4DataSource{} |
| 22 | +} |
| 23 | + |
| 24 | +type tenantV4DataSource struct { |
| 25 | + client *client.MeshStackProviderClient |
| 26 | +} |
| 27 | + |
| 28 | +func (d *tenantV4DataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { |
| 29 | + resp.TypeName = req.ProviderTypeName + "_tenant_v4" |
| 30 | +} |
| 31 | + |
| 32 | +func (d *tenantV4DataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { |
| 33 | + if req.ProviderData == nil { |
| 34 | + return |
| 35 | + } |
| 36 | + |
| 37 | + client, ok := req.ProviderData.(*client.MeshStackProviderClient) |
| 38 | + if !ok { |
| 39 | + resp.Diagnostics.AddError( |
| 40 | + "Unexpected Data Source Configure Type", |
| 41 | + fmt.Sprintf("Expected *client.MeshStackProviderClient, got: %T. Please report this issue to the provider developers.", req.ProviderData), |
| 42 | + ) |
| 43 | + return |
| 44 | + } |
| 45 | + |
| 46 | + d.client = client |
| 47 | +} |
| 48 | + |
| 49 | +func (d *tenantV4DataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { |
| 50 | + resp.Schema = schema.Schema{ |
| 51 | + MarkdownDescription: "Fetches details of a single tenant by UUID (v4).", |
| 52 | + Attributes: map[string]schema.Attribute{ |
| 53 | + "uuid": schema.StringAttribute{ |
| 54 | + MarkdownDescription: "UUID of the tenant.", |
| 55 | + Required: true, |
| 56 | + }, |
| 57 | + "metadata": schema.SingleNestedAttribute{ |
| 58 | + MarkdownDescription: "Tenant metadata.", |
| 59 | + Computed: true, |
| 60 | + Attributes: map[string]schema.Attribute{ |
| 61 | + "owned_by_workspace": schema.StringAttribute{ |
| 62 | + MarkdownDescription: "Identifier of the workspace the tenant belongs to.", |
| 63 | + Computed: true, |
| 64 | + }, |
| 65 | + "owned_by_project": schema.StringAttribute{ |
| 66 | + MarkdownDescription: "Identifier of the project the tenant belongs to.", |
| 67 | + Computed: true, |
| 68 | + }, |
| 69 | + "deleted_on": schema.StringAttribute{ |
| 70 | + MarkdownDescription: "If the tenant has been submitted for deletion by a workspace manager, the date is shown here.", |
| 71 | + Computed: true, |
| 72 | + }, |
| 73 | + "created_on": schema.StringAttribute{ |
| 74 | + MarkdownDescription: "The date the tenant was created.", |
| 75 | + Computed: true, |
| 76 | + }, |
| 77 | + }, |
| 78 | + }, |
| 79 | + "spec": schema.SingleNestedAttribute{ |
| 80 | + MarkdownDescription: "Tenant specification.", |
| 81 | + Computed: true, |
| 82 | + Attributes: map[string]schema.Attribute{ |
| 83 | + "platform_identifier": schema.StringAttribute{ |
| 84 | + MarkdownDescription: "Identifier of the target platform.", |
| 85 | + Computed: true, |
| 86 | + }, |
| 87 | + "local_id": schema.StringAttribute{ |
| 88 | + MarkdownDescription: "Tenant ID local to the platform.", |
| 89 | + Computed: true, |
| 90 | + }, |
| 91 | + "landing_zone_identifier": schema.StringAttribute{ |
| 92 | + MarkdownDescription: "Identifier of landing zone to assign to this tenant.", |
| 93 | + Computed: true, |
| 94 | + }, |
| 95 | + "quotas": schema.ListNestedAttribute{ |
| 96 | + MarkdownDescription: "Set of applied tenant quotas.", |
| 97 | + Computed: true, |
| 98 | + NestedObject: schema.NestedAttributeObject{ |
| 99 | + Attributes: map[string]schema.Attribute{ |
| 100 | + "key": schema.StringAttribute{Computed: true}, |
| 101 | + "value": schema.Int64Attribute{Computed: true}, |
| 102 | + }, |
| 103 | + }, |
| 104 | + }, |
| 105 | + }, |
| 106 | + }, |
| 107 | + "status": schema.SingleNestedAttribute{ |
| 108 | + MarkdownDescription: "Tenant status.", |
| 109 | + Computed: true, |
| 110 | + Attributes: map[string]schema.Attribute{ |
| 111 | + "tags": schema.MapAttribute{ |
| 112 | + MarkdownDescription: "Tags assigned to this tenant.", |
| 113 | + ElementType: types.ListType{ElemType: types.StringType}, |
| 114 | + Computed: true, |
| 115 | + }, |
| 116 | + "last_replicated": schema.StringAttribute{ |
| 117 | + MarkdownDescription: "The last time the tenant was replicated.", |
| 118 | + Computed: true, |
| 119 | + }, |
| 120 | + "current_replication_status": schema.StringAttribute{ |
| 121 | + MarkdownDescription: "The current replication status of the tenant.", |
| 122 | + Computed: true, |
| 123 | + }, |
| 124 | + }, |
| 125 | + }, |
| 126 | + }, |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +func (d *tenantV4DataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { |
| 131 | + var uuid string |
| 132 | + resp.Diagnostics.Append(req.Config.GetAttribute(ctx, path.Root("uuid"), &uuid)...) |
| 133 | + |
| 134 | + if resp.Diagnostics.HasError() { |
| 135 | + return |
| 136 | + } |
| 137 | + |
| 138 | + tenant, err := d.client.ReadTenantV4(uuid) |
| 139 | + if err != nil { |
| 140 | + resp.Diagnostics.AddError( |
| 141 | + "Error reading tenant", |
| 142 | + fmt.Sprintf("Could not read tenant, unexpected error: %s", err.Error()), |
| 143 | + ) |
| 144 | + return |
| 145 | + } |
| 146 | + |
| 147 | + if tenant == nil { |
| 148 | + resp.Diagnostics.AddError( |
| 149 | + "Error reading tenant", |
| 150 | + "Tenant not found", |
| 151 | + ) |
| 152 | + return |
| 153 | + } |
| 154 | + |
| 155 | + resp.Diagnostics.Append(resp.State.Set(ctx, tenant)...) |
| 156 | +} |
0 commit comments