|
| 1 | +// Copyright (c) ZStack.io, Inc. |
| 2 | + |
| 3 | +package provider |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "fmt" |
| 8 | + "terraform-provider-zstack/zstack/utils" |
| 9 | + |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/datasource" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 13 | + "zstack.io/zstack-sdk-go/pkg/client" |
| 14 | + "zstack.io/zstack-sdk-go/pkg/param" |
| 15 | +) |
| 16 | + |
| 17 | +var ( |
| 18 | + _ datasource.DataSource = &primaryStorageDataSource{} |
| 19 | + _ datasource.DataSourceWithConfigure = &primaryStorageDataSource{} |
| 20 | +) |
| 21 | + |
| 22 | +func ZStackPrimaryStorageDataSource() datasource.DataSource { |
| 23 | + return &primaryStorageDataSource{} |
| 24 | +} |
| 25 | + |
| 26 | +type primaryStorage struct { |
| 27 | + Name types.String `tfsdk:"name"` |
| 28 | + Uuid types.String `tfsdk:"uuid"` |
| 29 | + State types.String `tfsdk:"state"` |
| 30 | + Status types.String `tfsdk:"status"` |
| 31 | + TotalCapacity types.Int64 `tfsdk:"total_capacity"` |
| 32 | + AvailableCapacity types.Int64 `tfsdk:"available_capacity"` |
| 33 | + TotalPhysicalCapacity types.Int64 `tfsdk:"total_physical_capacity"` |
| 34 | + AvailablePhysicalCapacity types.Int64 `tfsdk:"available_physical_capacity"` |
| 35 | + SystemUsedCapacity types.Int64 `tfsdk:"system_used_capacity"` |
| 36 | +} |
| 37 | + |
| 38 | +type primaryStorageDataSourceModel struct { |
| 39 | + Name types.String `tfsdk:"name"` |
| 40 | + NamePattern types.String `tfsdk:"name_pattern"` |
| 41 | + Filter []Filter `tfsdk:"filter"` |
| 42 | + PrimaryStorges []primaryStorage `tfsdk:"primary_storages"` |
| 43 | +} |
| 44 | + |
| 45 | +type primaryStorageDataSource struct { |
| 46 | + client *client.ZSClient |
| 47 | +} |
| 48 | + |
| 49 | +// Configure implements datasource.DataSourceWithConfigure. |
| 50 | +func (d *primaryStorageDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { |
| 51 | + if req.ProviderData == nil { |
| 52 | + return |
| 53 | + } |
| 54 | + |
| 55 | + client, ok := req.ProviderData.(*client.ZSClient) |
| 56 | + if !ok { |
| 57 | + resp.Diagnostics.AddError( |
| 58 | + "Unexpected Data Source Configure Type", |
| 59 | + fmt.Sprintf("Expected *client.ZSClient, got: %T. Please report this issue to the Provider developer. ", req.ProviderData), |
| 60 | + ) |
| 61 | + |
| 62 | + return |
| 63 | + } |
| 64 | + d.client = client |
| 65 | +} |
| 66 | + |
| 67 | +// Metadata implements datasource.DataSourceWithConfigure. |
| 68 | +func (d *primaryStorageDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { |
| 69 | + resp.TypeName = req.ProviderTypeName + "_primary_storages" |
| 70 | +} |
| 71 | + |
| 72 | +// Read implements datasource.DataSourceWithConfigure. |
| 73 | +func (d *primaryStorageDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { |
| 74 | + var state primaryStorageDataSourceModel |
| 75 | + diags := req.Config.Get(ctx, &state) |
| 76 | + resp.Diagnostics.Append(diags...) |
| 77 | + |
| 78 | + if resp.Diagnostics.HasError() { |
| 79 | + return |
| 80 | + } |
| 81 | + |
| 82 | + params := param.NewQueryParam() |
| 83 | + |
| 84 | + if !state.Name.IsNull() { |
| 85 | + params.AddQ("name=" + state.Name.ValueString()) |
| 86 | + } else if !state.NamePattern.IsNull() { |
| 87 | + params.AddQ("name~=" + state.NamePattern.ValueString()) |
| 88 | + } |
| 89 | + |
| 90 | + primaryStorages, err := d.client.QueryPrimaryStorage(params) |
| 91 | + if err != nil { |
| 92 | + resp.Diagnostics.AddError( |
| 93 | + "Unable to Read ZStack primary Storages", |
| 94 | + err.Error(), |
| 95 | + ) |
| 96 | + return |
| 97 | + } |
| 98 | + |
| 99 | + filters := make(map[string][]string) |
| 100 | + for _, filter := range state.Filter { |
| 101 | + values := make([]string, 0, len(filter.Values.Elements())) |
| 102 | + diags := filter.Values.ElementsAs(ctx, &values, false) |
| 103 | + resp.Diagnostics.Append(diags...) |
| 104 | + if resp.Diagnostics.HasError() { |
| 105 | + return |
| 106 | + } |
| 107 | + filters[filter.Name.ValueString()] = values |
| 108 | + } |
| 109 | + |
| 110 | + filterPrimaryStorage, filterDiags := utils.FilterResource(ctx, primaryStorages, filters, "primary_storage") |
| 111 | + resp.Diagnostics.Append(filterDiags...) |
| 112 | + if resp.Diagnostics.HasError() { |
| 113 | + return |
| 114 | + } |
| 115 | + |
| 116 | + for _, primarystorage := range filterPrimaryStorage { |
| 117 | + primaryStorageState := primaryStorage{ |
| 118 | + TotalCapacity: types.Int64Value(primarystorage.TotalCapacity), |
| 119 | + State: types.StringValue(primarystorage.State), |
| 120 | + Status: types.StringValue(primarystorage.Status), |
| 121 | + Uuid: types.StringValue(primarystorage.UUID), |
| 122 | + AvailableCapacity: types.Int64Value(primarystorage.AvailableCapacity), |
| 123 | + Name: types.StringValue(primarystorage.Name), |
| 124 | + TotalPhysicalCapacity: types.Int64Value(primarystorage.TotalPhysicalCapacity), |
| 125 | + AvailablePhysicalCapacity: types.Int64Value(primarystorage.AvailablePhysicalCapacity), |
| 126 | + SystemUsedCapacity: types.Int64Value(primarystorage.SystemUsedCapacity), |
| 127 | + } |
| 128 | + |
| 129 | + state.PrimaryStorges = append(state.PrimaryStorges, primaryStorageState) |
| 130 | + } |
| 131 | + |
| 132 | + diags = resp.State.Set(ctx, &state) |
| 133 | + resp.Diagnostics.Append(diags...) |
| 134 | + if resp.Diagnostics.HasError() { |
| 135 | + return |
| 136 | + } |
| 137 | + |
| 138 | +} |
| 139 | + |
| 140 | +// Schema implements datasource.DataSourceWithConfigure. |
| 141 | +func (d *primaryStorageDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { |
| 142 | + resp.Schema = schema.Schema{ |
| 143 | + Description: "List all primary storages, or query primary storages by exact name match, or query primary storages by name pattern fuzzy match.", |
| 144 | + Attributes: map[string]schema.Attribute{ |
| 145 | + "name": schema.StringAttribute{ |
| 146 | + Description: "Exact name for searching primary storage.", |
| 147 | + Optional: true, |
| 148 | + }, |
| 149 | + "name_pattern": schema.StringAttribute{ |
| 150 | + Description: "Pattern for fuzzy name search, similar to MySQL LIKE. Use % for multiple characters and _ for exactly one character.", |
| 151 | + Optional: true, |
| 152 | + }, |
| 153 | + "primary_storages": schema.ListNestedAttribute{ |
| 154 | + Description: "List of primary storage entries", |
| 155 | + Computed: true, |
| 156 | + NestedObject: schema.NestedAttributeObject{ |
| 157 | + Attributes: map[string]schema.Attribute{ |
| 158 | + "name": schema.StringAttribute{ |
| 159 | + Description: "Name of the primary storage", |
| 160 | + Computed: true, |
| 161 | + }, |
| 162 | + |
| 163 | + "uuid": schema.StringAttribute{ |
| 164 | + Description: "UUID identifier of the primary storage", |
| 165 | + Computed: true, |
| 166 | + }, |
| 167 | + "state": schema.StringAttribute{ |
| 168 | + Description: "State of the primary storage (Enabled or Disabled)", |
| 169 | + Computed: true, |
| 170 | + }, |
| 171 | + "status": schema.StringAttribute{ |
| 172 | + Description: "Readiness status of the primary storage", |
| 173 | + Computed: true, |
| 174 | + }, |
| 175 | + "total_capacity": schema.Int64Attribute{ |
| 176 | + Description: "Total capacity of the primary storage in bytes", |
| 177 | + Computed: true, |
| 178 | + }, |
| 179 | + "available_capacity": schema.Int64Attribute{ |
| 180 | + Description: "Available capacity of the primary storage in bytes", |
| 181 | + Computed: true, |
| 182 | + }, |
| 183 | + "total_physical_capacity": schema.Int64Attribute{ |
| 184 | + Description: "Total physical capacity of the primary storage in bytes", |
| 185 | + Computed: true, |
| 186 | + }, |
| 187 | + "available_physical_capacity": schema.Int64Attribute{ |
| 188 | + Description: "Available physical capacity of the primary storage in bytes", |
| 189 | + Computed: true, |
| 190 | + }, |
| 191 | + "system_used_capacity": schema.Int64Attribute{ |
| 192 | + Description: "System used capacity of the primary storage in bytes", |
| 193 | + Computed: true, |
| 194 | + }, |
| 195 | + }, |
| 196 | + }, |
| 197 | + }, |
| 198 | + }, |
| 199 | + Blocks: map[string]schema.Block{ |
| 200 | + "filter": schema.ListNestedBlock{ |
| 201 | + Description: "Filter resources based on any field in the schema. For example, to filter by status, use `name = \"status\"` and `values = [\"Ready\"]`.", |
| 202 | + NestedObject: schema.NestedBlockObject{ |
| 203 | + Attributes: map[string]schema.Attribute{ |
| 204 | + "name": schema.StringAttribute{ |
| 205 | + Description: "Name of the field to filter by (e.g., status, state).", |
| 206 | + Required: true, |
| 207 | + }, |
| 208 | + "values": schema.SetAttribute{ |
| 209 | + Description: "Values to filter by. Multiple values will be treated as an OR condition.", |
| 210 | + Required: true, |
| 211 | + ElementType: types.StringType, |
| 212 | + }, |
| 213 | + }, |
| 214 | + }, |
| 215 | + }, |
| 216 | + }, |
| 217 | + } |
| 218 | +} |
0 commit comments