-
Notifications
You must be signed in to change notification settings - Fork 60
feat(telemetry link): Onboarding TelemetryLink #1456
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ozanichkovsky
wants to merge
10
commits into
stackitcloud:main
Choose a base branch
from
ozanichkovsky:feature/telemetrylink
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a39fce3
feat: TelemetryLink resources and datasources
db12dc1
chore: lint fix
a5447dd
Merge remote-tracking branch 'origin/main' into feature/telemetrylink
a085795
chore: renamed stackit_telemetrylink link to stackit_telemetrylink
4213072
chore: rename "TelemetryLink Link" to "TelemetryLink"
6d6d309
chore: sorted schema by required then optional
e573f59
chore: sorted schema fields by required then optional
d38ccae
chore: review comments fixed
9db81c2
chore: moved error handling out of the switch statement
a5f01a5
chore: partial update
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| data "stackit_telemetrylink" "link" { | ||
| resource_type = "project" | ||
| resource_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" | ||
| region = "eu01" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| resource "stackit_telemetrylink" "link" { | ||
| resource_type = "project" | ||
| resource_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" | ||
| region = "eu01" | ||
| display_name = "telemetrylink-example" | ||
| access_token = "eyJxxx" | ||
| telemetry_router_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" | ||
| } | ||
|
|
||
| resource "stackit_telemetrylink" "link2" { | ||
| resource_type = "project" | ||
| resource_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" | ||
| region = "eu01" | ||
| display_name = "telemetrylink-example" | ||
| description = "telemetrylink description" | ||
| access_token = "eyJxxx" | ||
| telemetry_router_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" | ||
| } | ||
|
|
||
| # Only use the import statement, if you want to import an existing TelemetryLink | ||
| import { | ||
| to = stackit_telemetrylink.import-example | ||
| id = "${var.resource_type},${var.resource_id},${var.region}" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
212 changes: 212 additions & 0 deletions
212
stackit/internal/services/telemetrylink/link/datasource.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,212 @@ | ||
| package link | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "net/http" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" | ||
| "github.com/hashicorp/terraform-plugin-framework/datasource" | ||
| "github.com/hashicorp/terraform-plugin-framework/datasource/schema" | ||
| "github.com/hashicorp/terraform-plugin-framework/schema/validator" | ||
| "github.com/hashicorp/terraform-plugin-framework/types" | ||
| "github.com/hashicorp/terraform-plugin-log/tflog" | ||
| "github.com/stackitcloud/stackit-sdk-go/core/oapierror" | ||
|
|
||
| telemetrylink "github.com/stackitcloud/stackit-sdk-go/services/telemetrylink/v1betaapi" | ||
|
|
||
| "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion" | ||
| "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core" | ||
| "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/telemetrylink/utils" | ||
| tfutils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils" | ||
| "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate" | ||
| ) | ||
|
|
||
| var ( | ||
| _ datasource.DataSource = &telemetryLinkLinkDataSource{} | ||
| ) | ||
|
|
||
| func NewTelemetryLinkLinkDataSource() datasource.DataSource { | ||
| return &telemetryLinkLinkDataSource{} | ||
| } | ||
|
|
||
| type DataSourceModel struct { | ||
| ID types.String `tfsdk:"id"` // Required by Terraform | ||
| LinkID types.String `tfsdk:"link_id"` | ||
| Region types.String `tfsdk:"region"` | ||
| ResourceType types.String `tfsdk:"resource_type"` | ||
| ResourceID types.String `tfsdk:"resource_id"` | ||
| DisplayName types.String `tfsdk:"display_name"` | ||
| Description types.String `tfsdk:"description"` | ||
| TelemetryRouterID types.String `tfsdk:"telemetry_router_id"` | ||
| CreateTime types.String `tfsdk:"create_time"` | ||
| Status types.String `tfsdk:"status"` | ||
| } | ||
|
|
||
| type telemetryLinkLinkDataSource struct { | ||
| client *telemetrylink.APIClient | ||
| providerData core.ProviderData | ||
| } | ||
|
|
||
| func (d *telemetryLinkLinkDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { | ||
| resp.TypeName = req.ProviderTypeName + "_telemetrylink" | ||
| } | ||
|
|
||
| func (d *telemetryLinkLinkDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { | ||
| providerData, ok := conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics) | ||
| if !ok { | ||
| return | ||
| } | ||
| d.providerData = providerData | ||
|
|
||
| apiClient := utils.ConfigureClient(ctx, &providerData, &resp.Diagnostics) | ||
| if resp.Diagnostics.HasError() { | ||
| return | ||
| } | ||
| d.client = apiClient | ||
| tflog.Info(ctx, "TelemetryLink client configured") | ||
| } | ||
|
|
||
| func (d *telemetryLinkLinkDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { | ||
| resp.Schema = schema.Schema{ | ||
| Description: fmt.Sprintf("TelemetryLink data source schema. %s", core.DatasourceRegionFallbackDocstring), | ||
| Attributes: map[string]schema.Attribute{ | ||
| "id": schema.StringAttribute{ | ||
| Description: schemaDescriptions["id"], | ||
| Computed: true, | ||
| }, | ||
| "link_id": schema.StringAttribute{ | ||
| Description: schemaDescriptions["instance_id"], | ||
| Computed: true, | ||
| Validators: []validator.String{ | ||
| validate.UUID(), | ||
| validate.NoSeparator(), | ||
| }, | ||
| }, | ||
| "resource_type": schema.StringAttribute{ | ||
| Description: schemaDescriptions["resource_type"], | ||
| Required: true, | ||
| Validators: []validator.String{ | ||
| stringvalidator.OneOf(resourceTypes...), | ||
| validate.NoSeparator(), | ||
| }, | ||
| }, | ||
| "resource_id": schema.StringAttribute{ | ||
| Description: schemaDescriptions["resource_id"], | ||
| Required: true, | ||
| Validators: []validator.String{ | ||
| validate.UUID(), | ||
| validate.NoSeparator(), | ||
| }, | ||
| }, | ||
| "region": schema.StringAttribute{ | ||
| Description: schemaDescriptions["region"], | ||
| // the region cannot be found, so it has to be passed | ||
| Optional: true, | ||
| }, | ||
| "display_name": schema.StringAttribute{ | ||
| Description: schemaDescriptions["display_name"], | ||
| Computed: true, | ||
| Validators: []validator.String{stringvalidator.LengthAtLeast(1)}, | ||
| }, | ||
| "description": schema.StringAttribute{ | ||
| Description: schemaDescriptions["description"], | ||
| Computed: true, | ||
| }, | ||
| "telemetry_router_id": schema.StringAttribute{ | ||
| Description: schemaDescriptions["telemetry_router_id"], | ||
| Computed: true, | ||
| }, | ||
| "create_time": schema.StringAttribute{ | ||
| Description: schemaDescriptions["create_time"], | ||
| Computed: true, | ||
| }, | ||
| "status": schema.StringAttribute{ | ||
| Description: schemaDescriptions["status"], | ||
| Computed: true, | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func (d *telemetryLinkLinkDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { // nolint:gocritic // function signature required by Terraform | ||
| var model DataSourceModel | ||
| diags := req.Config.Get(ctx, &model) | ||
| resp.Diagnostics.Append(diags...) | ||
| if resp.Diagnostics.HasError() { | ||
| return | ||
| } | ||
|
|
||
| ctx = core.InitProviderContext(ctx) | ||
|
|
||
| resourceType := model.ResourceType.ValueString() | ||
| resourceID := model.ResourceID.ValueString() | ||
| region := d.providerData.GetRegionWithOverride(model.Region) | ||
|
|
||
| ctx = tflog.SetField(ctx, "resource_type", resourceType) | ||
| ctx = tflog.SetField(ctx, "resource_id", resourceID) | ||
| ctx = tflog.SetField(ctx, "region", region) | ||
|
|
||
| var response *telemetrylink.TelemetryLinkResponse | ||
| var err error | ||
| switch resourceType { | ||
| case resourceTypeOrganization: | ||
| response, err = d.client.DefaultAPI.GetOrganizationTelemetryLink(ctx, resourceID, region).Execute() | ||
| case resourceTypeFolder: | ||
| response, err = d.client.DefaultAPI.GetFolderTelemetryLink(ctx, resourceID, region).Execute() | ||
| case resourceTypeProject: | ||
| response, err = d.client.DefaultAPI.GetProjectTelemetryLink(ctx, resourceID, region).Execute() | ||
| } | ||
| if err != nil { | ||
| var oapiErr *oapierror.GenericOpenAPIError | ||
| ok := errors.As(err, &oapiErr) | ||
| if ok && oapiErr.StatusCode == http.StatusNotFound { | ||
| resp.State.RemoveResource(ctx) | ||
| return | ||
| } | ||
| core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading TelemetryLink", fmt.Sprintf("Calling API: %v", err)) | ||
| return | ||
| } | ||
| ctx = core.LogResponse(ctx) | ||
|
|
||
| err = mapDataSourceFields(ctx, response, &model) | ||
| if err != nil { | ||
| core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading TelemetryLink", fmt.Sprintf("Processing response: %v", err)) | ||
| return | ||
| } | ||
| diags = resp.State.Set(ctx, model) | ||
| resp.Diagnostics.Append(diags...) | ||
| if resp.Diagnostics.HasError() { | ||
| return | ||
| } | ||
| tflog.Info(ctx, "TelemetryLink read", map[string]interface{}{ | ||
| "resource_type": resourceType, | ||
| "resource_id": resourceID, | ||
| }) | ||
| } | ||
|
|
||
| func mapDataSourceFields(_ context.Context, link *telemetrylink.TelemetryLinkResponse, model *DataSourceModel) error { | ||
| if link == nil { | ||
| return fmt.Errorf("link is nil") | ||
| } | ||
| if model == nil { | ||
| return fmt.Errorf("model is nil") | ||
| } | ||
| var linkID string | ||
| if model.LinkID.ValueString() != "" { | ||
| linkID = model.LinkID.ValueString() | ||
| } else { | ||
| linkID = link.Id | ||
| } | ||
|
|
||
| model.ID = tfutils.BuildInternalTerraformId(model.ResourceType.ValueString(), model.ResourceID.ValueString(), model.Region.ValueString()) | ||
| model.LinkID = types.StringValue(linkID) | ||
| model.DisplayName = types.StringValue(link.DisplayName) | ||
| model.Description = types.StringPointerValue(link.Description) | ||
| model.TelemetryRouterID = types.StringValue(link.TelemetryRouterId) | ||
| model.CreateTime = types.StringValue(link.CreateTime.String()) | ||
| model.Status = types.StringValue(link.Status) | ||
|
|
||
| return nil | ||
| } | ||
85 changes: 85 additions & 0 deletions
85
stackit/internal/services/telemetrylink/link/datasource_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| package link | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/google/go-cmp/cmp" | ||
| "github.com/hashicorp/terraform-plugin-framework/types" | ||
| telemetrylink "github.com/stackitcloud/stackit-sdk-go/services/telemetrylink/v1betaapi" | ||
| ) | ||
|
|
||
| func fixtureDataSourceModel(mods ...func(model *DataSourceModel)) *DataSourceModel { | ||
| model := &DataSourceModel{ | ||
| ID: types.StringValue("rtp,rid,reg"), | ||
| LinkID: types.StringValue("lid"), | ||
| Region: types.StringValue("reg"), | ||
| ResourceType: types.StringValue("rtp"), | ||
| ResourceID: types.StringValue("rid"), | ||
| DisplayName: types.StringValue("name"), | ||
| Description: types.String{}, | ||
| TelemetryRouterID: types.StringValue("tlmrid"), | ||
| CreateTime: types.StringValue(testTime.String()), | ||
| Status: types.StringValue("active"), | ||
| } | ||
| for _, mod := range mods { | ||
| mod(model) | ||
| } | ||
| return model | ||
| } | ||
|
|
||
| func TestMapDataSourceFields(t *testing.T) { | ||
| tests := []struct { | ||
| description string | ||
| input *telemetrylink.TelemetryLinkResponse | ||
| expected *DataSourceModel | ||
| wantErr bool | ||
| }{ | ||
| { | ||
| description: "min values", | ||
| input: fixtureLink(), | ||
| expected: fixtureDataSourceModel(), | ||
| }, | ||
| { | ||
| description: "max values", | ||
| input: fixtureLink(func(link *telemetrylink.TelemetryLinkResponse) { | ||
| link.Description = new("description") | ||
| link.DisplayName = "display-name" | ||
| link.AccessToken = new("access-token") | ||
| link.TelemetryRouterId = "tlmr-id" | ||
| }), | ||
| expected: fixtureDataSourceModel(func(model *DataSourceModel) { | ||
| model.Description = types.StringValue("description") | ||
| model.DisplayName = types.StringValue("display-name") | ||
| model.TelemetryRouterID = types.StringValue("tlmr-id") | ||
| }), | ||
| }, | ||
| { | ||
| description: "nil input", | ||
| wantErr: true, | ||
| expected: fixtureDataSourceModel(), | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.description, func(t *testing.T) { | ||
| state := &DataSourceModel{ | ||
| ResourceType: tt.expected.ResourceType, | ||
| ResourceID: tt.expected.ResourceID, | ||
| Region: tt.expected.Region, | ||
| } | ||
| err := mapDataSourceFields(context.Background(), tt.input, state) | ||
| if tt.wantErr && err == nil { | ||
| t.Fatalf("Should have failed") | ||
| } | ||
| if !tt.wantErr && err != nil { | ||
| t.Fatalf("Should not have failed: %v", err) | ||
| } | ||
| if !tt.wantErr { | ||
| diff := cmp.Diff(state, tt.expected) | ||
| if diff != "" { | ||
| t.Fatalf("Data does not match: %s", diff) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.