|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/cockroachdb/cockroach-cloud-sdk-go/v6/pkg/client" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/datasource" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/schema/validator" |
| 14 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 15 | +) |
| 16 | + |
| 17 | +type blackoutWindowDataSource struct { |
| 18 | + provider *provider |
| 19 | +} |
| 20 | + |
| 21 | +func NewBlackoutWindowDataSource() datasource.DataSource { |
| 22 | + return &blackoutWindowDataSource{} |
| 23 | +} |
| 24 | + |
| 25 | +func (d *blackoutWindowDataSource) Schema( |
| 26 | + _ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse, |
| 27 | +) { |
| 28 | + resp.Schema = schema.Schema{ |
| 29 | + Description: "Retrieve blackout windows defined for a CockroachDB Cloud cluster.", |
| 30 | + Attributes: map[string]schema.Attribute{ |
| 31 | + "cluster_id": schema.StringAttribute{ |
| 32 | + MarkdownDescription: "ID of the cluster to query for blackout windows.", |
| 33 | + Required: true, |
| 34 | + Validators: uuidValidator, |
| 35 | + }, |
| 36 | + "page": schema.StringAttribute{ |
| 37 | + Optional: true, |
| 38 | + MarkdownDescription: "A pagination token used to request the next page of results. This value should come from the `next_page` attribute of a previous data source call.", |
| 39 | + }, |
| 40 | + "sort_order": schema.StringAttribute{ |
| 41 | + Optional: true, |
| 42 | + MarkdownDescription: "Specifies the sort direction for the returned results, which are ordered based on the `start_time` field. Use `ASC` for ascending or `DESC` for descending order. Defaults to `ASC`.", |
| 43 | + Validators: []validator.String{stringvalidator.OneOf("ASC", "DESC")}, |
| 44 | + }, |
| 45 | + "limit": schema.Int32Attribute{ |
| 46 | + Optional: true, |
| 47 | + MarkdownDescription: "Maximum number of blackout windows to return in a single response. Defaults to 100 when not set.", |
| 48 | + }, |
| 49 | + "next_page": schema.StringAttribute{ |
| 50 | + Computed: true, |
| 51 | + MarkdownDescription: "Pagination token for the next page of results returned by the API. Pass this value into the `page` argument in a subsequent data source call to retrieve the next set of blackout windows. Empty if there are no more pages.", |
| 52 | + }, |
| 53 | + "blackout_windows": schema.ListNestedAttribute{ |
| 54 | + Computed: true, |
| 55 | + MarkdownDescription: "List of blackout windows returned by the API.", |
| 56 | + NestedObject: schema.NestedAttributeObject{ |
| 57 | + Attributes: map[string]schema.Attribute{ |
| 58 | + "cluster_id": schema.StringAttribute{ |
| 59 | + Computed: true, |
| 60 | + MarkdownDescription: "ID of the cluster the blackout window belongs to.", |
| 61 | + }, |
| 62 | + "id": schema.StringAttribute{ |
| 63 | + Computed: true, |
| 64 | + MarkdownDescription: "Unique blackout window identifier.", |
| 65 | + }, |
| 66 | + "start_time": schema.StringAttribute{ |
| 67 | + Computed: true, |
| 68 | + MarkdownDescription: "UTC start time in RFC3339 format (e.g. `2025-03-15T09:00:00Z`).", |
| 69 | + }, |
| 70 | + "end_time": schema.StringAttribute{ |
| 71 | + Computed: true, |
| 72 | + MarkdownDescription: "UTC end time in RFC3339 format (e.g. `2025-03-18T09:00:00Z`).", |
| 73 | + }, |
| 74 | + }, |
| 75 | + }, |
| 76 | + }, |
| 77 | + }, |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +func (d *blackoutWindowDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { |
| 82 | + resp.TypeName = req.ProviderTypeName + "_blackout_windows" |
| 83 | +} |
| 84 | + |
| 85 | +func (d *blackoutWindowDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { |
| 86 | + if req.ProviderData == nil { |
| 87 | + return |
| 88 | + } |
| 89 | + var ok bool |
| 90 | + if d.provider, ok = req.ProviderData.(*provider); !ok { |
| 91 | + resp.Diagnostics.AddError( |
| 92 | + "Internal provider error", |
| 93 | + fmt.Sprintf("Error in Configure: expected %T but got %T", provider{}, req.ProviderData), |
| 94 | + ) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +func (d *blackoutWindowDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { |
| 99 | + if d.provider == nil || !d.provider.configured { |
| 100 | + addConfigureProviderErr(&resp.Diagnostics) |
| 101 | + return |
| 102 | + } |
| 103 | + |
| 104 | + var listConfig BlackoutWindowList |
| 105 | + diags := req.Config.Get(ctx, &listConfig) |
| 106 | + |
| 107 | + resp.Diagnostics.Append(diags...) |
| 108 | + if resp.Diagnostics.HasError() { |
| 109 | + resp.Diagnostics.AddWarning("Error loading the blackout window list", "") |
| 110 | + return |
| 111 | + } |
| 112 | + |
| 113 | + clusterID := listConfig.ClusterID.ValueString() |
| 114 | + |
| 115 | + listBlackoutWindowsOptions := &client.ListBlackoutWindowsOptions{} |
| 116 | + |
| 117 | + if IsKnown(listConfig.Limit) { |
| 118 | + listBlackoutWindowsOptions.PaginationLimit = listConfig.Limit.ValueInt32Pointer() |
| 119 | + } |
| 120 | + |
| 121 | + if IsKnown(listConfig.Page) { |
| 122 | + page := strings.TrimSpace(listConfig.Page.ValueString()) |
| 123 | + if page != "" { |
| 124 | + listBlackoutWindowsOptions.PaginationPage = &page |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + if IsKnown(listConfig.SortOrder) { |
| 129 | + sortOrder := strings.ToUpper(strings.TrimSpace(listConfig.SortOrder.ValueString())) |
| 130 | + if sortOrder != "" { |
| 131 | + listBlackoutWindowsOptions.PaginationSortOrder = &sortOrder |
| 132 | + listConfig.SortOrder = types.StringValue(sortOrder) |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + traceAPICall("ListBlackoutWindows") |
| 137 | + listResp, _, err := d.provider.service.ListBlackoutWindows(ctx, clusterID, listBlackoutWindowsOptions) |
| 138 | + if err != nil || listResp == nil { |
| 139 | + resp.Diagnostics.AddError( |
| 140 | + "Error listing blackout windows", |
| 141 | + fmt.Sprintf("Unexpected error retrieving blackout windows: %s", formatAPIErrorMessage(err)), |
| 142 | + ) |
| 143 | + return |
| 144 | + } |
| 145 | + |
| 146 | + listConfig.BlackoutWindows = make([]BlackoutWindow, 0) |
| 147 | + if listResp.BlackoutWindows != nil { |
| 148 | + for _, window := range *listResp.BlackoutWindows { |
| 149 | + listConfig.BlackoutWindows = append(listConfig.BlackoutWindows, BlackoutWindow{ |
| 150 | + ClusterID: types.StringValue(window.GetClusterId()), |
| 151 | + ID: types.StringValue(window.GetId()), |
| 152 | + StartTime: types.StringValue(window.GetStartTime().UTC().Format(time.RFC3339)), |
| 153 | + EndTime: types.StringValue(window.GetEndTime().UTC().Format(time.RFC3339)), |
| 154 | + }) |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + if listResp.Pagination != nil && listResp.Pagination.NextPage != nil && *listResp.Pagination.NextPage != "" { |
| 159 | + listConfig.NextPage = types.StringValue(*listResp.Pagination.NextPage) |
| 160 | + } else { |
| 161 | + listConfig.NextPage = types.StringNull() |
| 162 | + } |
| 163 | + |
| 164 | + diags = resp.State.Set(ctx, listConfig) |
| 165 | + resp.Diagnostics.Append(diags...) |
| 166 | +} |
0 commit comments