|
| 1 | +package link |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/datasource" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/schema/validator" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 14 | + "github.com/hashicorp/terraform-plugin-log/tflog" |
| 15 | + telemetrylink "github.com/stackitcloud/stackit-sdk-go/services/telemetrylink/v1betaapi" |
| 16 | + |
| 17 | + "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion" |
| 18 | + "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core" |
| 19 | + "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/telemetrylink/utils" |
| 20 | + tfutils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils" |
| 21 | + "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate" |
| 22 | +) |
| 23 | + |
| 24 | +var ( |
| 25 | + _ datasource.DataSource = &telemetryLinkDataSource{} |
| 26 | +) |
| 27 | + |
| 28 | +func NewTelemetryLinkDataSource() datasource.DataSource { |
| 29 | + return &telemetryLinkDataSource{} |
| 30 | +} |
| 31 | + |
| 32 | +type DataSourceModel struct { |
| 33 | + ID types.String `tfsdk:"id"` // Required by Terraform |
| 34 | + Region types.String `tfsdk:"region"` |
| 35 | + ResourceType types.String `tfsdk:"resource_type"` |
| 36 | + ResourceID types.String `tfsdk:"resource_id"` |
| 37 | + DisplayName types.String `tfsdk:"display_name"` |
| 38 | + Description types.String `tfsdk:"description"` |
| 39 | + TelemetryRouterID types.String `tfsdk:"telemetry_router_id"` |
| 40 | + CreateTime types.String `tfsdk:"create_time"` |
| 41 | + Status types.String `tfsdk:"status"` |
| 42 | +} |
| 43 | + |
| 44 | +type telemetryLinkDataSource struct { |
| 45 | + client *telemetrylink.APIClient |
| 46 | + providerData core.ProviderData |
| 47 | +} |
| 48 | + |
| 49 | +func (d *telemetryLinkDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { |
| 50 | + resp.TypeName = req.ProviderTypeName + "_telemetrylink" |
| 51 | +} |
| 52 | + |
| 53 | +func (d *telemetryLinkDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { |
| 54 | + providerData, ok := conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics) |
| 55 | + if !ok { |
| 56 | + return |
| 57 | + } |
| 58 | + d.providerData = providerData |
| 59 | + |
| 60 | + apiClient := utils.ConfigureClient(ctx, &providerData, &resp.Diagnostics) |
| 61 | + if resp.Diagnostics.HasError() { |
| 62 | + return |
| 63 | + } |
| 64 | + d.client = apiClient |
| 65 | + tflog.Info(ctx, "TelemetryLink client configured") |
| 66 | +} |
| 67 | + |
| 68 | +func (d *telemetryLinkDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { |
| 69 | + resp.Schema = schema.Schema{ |
| 70 | + Description: fmt.Sprintf("TelemetryLink data source schema. %s", core.DatasourceRegionFallbackDocstring), |
| 71 | + Attributes: map[string]schema.Attribute{ |
| 72 | + "id": schema.StringAttribute{ |
| 73 | + Description: schemaDescriptions["id"], |
| 74 | + Computed: true, |
| 75 | + }, |
| 76 | + "resource_type": schema.StringAttribute{ |
| 77 | + Description: schemaDescriptions["resource_type"], |
| 78 | + Required: true, |
| 79 | + Validators: []validator.String{ |
| 80 | + stringvalidator.OneOf(resourceTypes...), |
| 81 | + validate.NoSeparator(), |
| 82 | + }, |
| 83 | + }, |
| 84 | + "resource_id": schema.StringAttribute{ |
| 85 | + Description: schemaDescriptions["resource_id"], |
| 86 | + Required: true, |
| 87 | + Validators: []validator.String{ |
| 88 | + validate.UUID(), |
| 89 | + validate.NoSeparator(), |
| 90 | + }, |
| 91 | + }, |
| 92 | + "region": schema.StringAttribute{ |
| 93 | + Description: schemaDescriptions["region"], |
| 94 | + // the region cannot be found, so it has to be passed |
| 95 | + Optional: true, |
| 96 | + }, |
| 97 | + "display_name": schema.StringAttribute{ |
| 98 | + Description: schemaDescriptions["display_name"], |
| 99 | + Computed: true, |
| 100 | + }, |
| 101 | + "description": schema.StringAttribute{ |
| 102 | + Description: schemaDescriptions["description"], |
| 103 | + Computed: true, |
| 104 | + }, |
| 105 | + "telemetry_router_id": schema.StringAttribute{ |
| 106 | + Description: schemaDescriptions["telemetry_router_id"], |
| 107 | + Computed: true, |
| 108 | + }, |
| 109 | + "create_time": schema.StringAttribute{ |
| 110 | + Description: schemaDescriptions["create_time"], |
| 111 | + Computed: true, |
| 112 | + }, |
| 113 | + "status": schema.StringAttribute{ |
| 114 | + Description: schemaDescriptions["status"], |
| 115 | + Computed: true, |
| 116 | + }, |
| 117 | + }, |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +func (d *telemetryLinkDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { // nolint:gocritic // function signature required by Terraform |
| 122 | + var model DataSourceModel |
| 123 | + diags := req.Config.Get(ctx, &model) |
| 124 | + resp.Diagnostics.Append(diags...) |
| 125 | + if resp.Diagnostics.HasError() { |
| 126 | + return |
| 127 | + } |
| 128 | + |
| 129 | + ctx = core.InitProviderContext(ctx) |
| 130 | + |
| 131 | + resourceType := model.ResourceType.ValueString() |
| 132 | + resourceID := model.ResourceID.ValueString() |
| 133 | + region := d.providerData.GetRegionWithOverride(model.Region) |
| 134 | + |
| 135 | + ctx = tflog.SetField(ctx, "resource_type", resourceType) |
| 136 | + ctx = tflog.SetField(ctx, "resource_id", resourceID) |
| 137 | + ctx = tflog.SetField(ctx, "region", region) |
| 138 | + |
| 139 | + var response *telemetrylink.TelemetryLinkResponse |
| 140 | + var err error |
| 141 | + switch resourceType { |
| 142 | + case resourceTypeOrganization: |
| 143 | + response, err = d.client.DefaultAPI.GetOrganizationTelemetryLink(ctx, resourceID, region).Execute() |
| 144 | + case resourceTypeFolder: |
| 145 | + response, err = d.client.DefaultAPI.GetFolderTelemetryLink(ctx, resourceID, region).Execute() |
| 146 | + case resourceTypeProject: |
| 147 | + response, err = d.client.DefaultAPI.GetProjectTelemetryLink(ctx, resourceID, region).Execute() |
| 148 | + default: |
| 149 | + core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading TelemetryLink", fmt.Sprintf("Unsupported resource type: %s", resourceType)) |
| 150 | + return |
| 151 | + } |
| 152 | + if err != nil { |
| 153 | + tfutils.LogError( |
| 154 | + ctx, |
| 155 | + &resp.Diagnostics, |
| 156 | + err, |
| 157 | + "Error reading TelemetryLink", |
| 158 | + fmt.Sprintf("TelemetryLink for resource type %q and resource ID %q does not exist.", resourceType, resourceID), |
| 159 | + map[int]string{ |
| 160 | + http.StatusForbidden: fmt.Sprintf("Resource with type %q ID %q not found or forbidden access", resourceType, resourceID), |
| 161 | + }, |
| 162 | + ) |
| 163 | + resp.State.RemoveResource(ctx) |
| 164 | + return |
| 165 | + } |
| 166 | + ctx = core.LogResponse(ctx) |
| 167 | + |
| 168 | + err = mapDataSourceFields(ctx, response, &model, region) |
| 169 | + if err != nil { |
| 170 | + core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading TelemetryLink", fmt.Sprintf("Processing response: %v", err)) |
| 171 | + return |
| 172 | + } |
| 173 | + diags = resp.State.Set(ctx, model) |
| 174 | + resp.Diagnostics.Append(diags...) |
| 175 | + if resp.Diagnostics.HasError() { |
| 176 | + return |
| 177 | + } |
| 178 | + tflog.Info(ctx, "TelemetryLink read", map[string]interface{}{ |
| 179 | + "resource_type": resourceType, |
| 180 | + "resource_id": resourceID, |
| 181 | + }) |
| 182 | +} |
| 183 | + |
| 184 | +func mapDataSourceFields(_ context.Context, link *telemetrylink.TelemetryLinkResponse, model *DataSourceModel, region string) error { |
| 185 | + if link == nil { |
| 186 | + return fmt.Errorf("link is nil") |
| 187 | + } |
| 188 | + if model == nil { |
| 189 | + return fmt.Errorf("model is nil") |
| 190 | + } |
| 191 | + |
| 192 | + model.ID = tfutils.BuildInternalTerraformId(model.ResourceType.ValueString(), model.ResourceID.ValueString(), region) |
| 193 | + model.Region = types.StringValue(region) |
| 194 | + model.DisplayName = types.StringValue(link.DisplayName) |
| 195 | + model.Description = types.StringPointerValue(link.Description) |
| 196 | + model.TelemetryRouterID = types.StringValue(link.TelemetryRouterId) |
| 197 | + model.CreateTime = types.StringValue(link.CreateTime.Format(time.RFC3339)) |
| 198 | + model.Status = types.StringValue(string(link.Status)) |
| 199 | + |
| 200 | + return nil |
| 201 | +} |
0 commit comments