-
Notifications
You must be signed in to change notification settings - Fork 61
feat(telemetryrouter): Onboarding TelemetryRouter #1425
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
15
commits into
stackitcloud:main
Choose a base branch
from
ozanichkovsky:feature/telemetryrouter
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 6 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
f21e236
feat: TelemetryRouter resources and datasources
3b1b2b0
chore: make fmt
0f2348b
chore: fixed lint errors
ef9fd7d
feat: added TelemetryRouter waiters
be52841
Merge branch 'main' of github.com:ozanichkovsky/terraform-provider-st…
3a12b56
Merge remote-tracking branch 'origin/main' into feature/telemetryrouter
94eb4b7
Merge remote-tracking branch 'origin/main' into feature/telemetryrouter
becf3e7
Update stackit/internal/services/telemetryrouter/instance/resource.go
ozanichkovsky 89eb889
chore: fixes according to code review comments
f162ec0
Update stackit/internal/services/telemetryrouter/accesstoken/resource.go
ozanichkovsky cf8eaa6
chore: combine access token resource mapFields into a single function
b10cec2
Merge remote-tracking branch 'origin/main' into feature/telemetryrouter
4d75ae4
feat: use real resources for acceptance tests
d28280a
Merge remote-tracking branch 'origin/main' into feature/telemetryrouter
861200e
fix: acceptance test
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
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
217 changes: 217 additions & 0 deletions
217
stackit/internal/services/telemetryrouter/accesstoken/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,217 @@ | ||
| package accesstoken | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "net/http" | ||
| "time" | ||
|
|
||
| "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" | ||
| telemetryrouter "github.com/stackitcloud/stackit-sdk-go/services/telemetryrouter/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/telemetryrouter/utils" | ||
| tfutils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils" | ||
| "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate" | ||
| ) | ||
|
|
||
| var ( | ||
| _ datasource.DataSource = &telemetryRouterAccessTokenDataSource{} | ||
| _ datasource.DataSourceWithConfigure = &telemetryRouterAccessTokenDataSource{} | ||
| ) | ||
|
|
||
| type DataSourceModel struct { | ||
| ID types.String `tfsdk:"id"` // Required by Terraform | ||
| AccessTokenID types.String `tfsdk:"access_token_id"` | ||
| InstanceID types.String `tfsdk:"instance_id"` | ||
| Region types.String `tfsdk:"region"` | ||
| ProjectID types.String `tfsdk:"project_id"` | ||
| CreatorID types.String `tfsdk:"creator_id"` | ||
| Description types.String `tfsdk:"description"` | ||
| DisplayName types.String `tfsdk:"display_name"` | ||
| ExpirationTime types.String `tfsdk:"expiration_time"` | ||
| Status types.String `tfsdk:"status"` | ||
| } | ||
|
|
||
| func NewTelemetryRouterAccessTokenDataSource() datasource.DataSource { | ||
| return &telemetryRouterAccessTokenDataSource{} | ||
| } | ||
|
|
||
| type telemetryRouterAccessTokenDataSource struct { | ||
| client *telemetryrouter.APIClient | ||
| providerData core.ProviderData | ||
| } | ||
|
|
||
| func (d *telemetryRouterAccessTokenDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { | ||
| resp.TypeName = req.ProviderTypeName + "_telemetryrouter_access_token" | ||
| } | ||
|
|
||
| func (d *telemetryRouterAccessTokenDataSource) 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, "TelemetryRouter client configured") | ||
| } | ||
|
|
||
| func (d *telemetryRouterAccessTokenDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { | ||
| resp.Schema = schema.Schema{ | ||
| Description: fmt.Sprintf("TelemetryRouter access token data source schema. %s", core.DatasourceRegionFallbackDocstring), | ||
| Attributes: map[string]schema.Attribute{ | ||
| "id": schema.StringAttribute{ | ||
| Description: schemaDescriptions["id"], | ||
| Computed: true, | ||
| }, | ||
| "access_token_id": schema.StringAttribute{ | ||
| Description: schemaDescriptions["access_token_id"], | ||
| Required: true, | ||
| Validators: []validator.String{ | ||
| validate.UUID(), | ||
| validate.NoSeparator(), | ||
| }, | ||
| }, | ||
| "instance_id": schema.StringAttribute{ | ||
| Description: schemaDescriptions["instance_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, | ||
| }, | ||
| "project_id": schema.StringAttribute{ | ||
| Description: schemaDescriptions["project_id"], | ||
| Required: true, | ||
| Validators: []validator.String{ | ||
| validate.UUID(), | ||
| validate.NoSeparator(), | ||
| }, | ||
| }, | ||
| "creator_id": schema.StringAttribute{ | ||
| Description: schemaDescriptions["creator_id"], | ||
| Computed: true, | ||
| }, | ||
| "description": schema.StringAttribute{ | ||
| Description: schemaDescriptions["description"], | ||
| Computed: true, | ||
| }, | ||
| "display_name": schema.StringAttribute{ | ||
| Description: schemaDescriptions["display_name"], | ||
| Computed: true, | ||
| Validators: []validator.String{stringvalidator.LengthAtLeast(1)}, | ||
| }, | ||
| "expiration_time": schema.StringAttribute{ | ||
| Description: schemaDescriptions["expiration_time"], | ||
| Computed: true, | ||
| }, | ||
| "status": schema.StringAttribute{ | ||
| Description: schemaDescriptions["status"], | ||
| Computed: true, | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func (d *telemetryRouterAccessTokenDataSource) 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) | ||
|
|
||
| projectID := model.ProjectID.ValueString() | ||
| region := d.providerData.GetRegionWithOverride(model.Region) | ||
| instanceID := model.InstanceID.ValueString() | ||
| accessTokenID := model.AccessTokenID.ValueString() | ||
|
|
||
| ctx = tflog.SetField(ctx, "project_id", projectID) | ||
| ctx = tflog.SetField(ctx, "region", region) | ||
| ctx = tflog.SetField(ctx, "instance_id", instanceID) | ||
| ctx = tflog.SetField(ctx, "access_token_id", accessTokenID) | ||
|
|
||
| accessTokenResponse, err := d.client.DefaultAPI.GetAccessToken(ctx, projectID, region, instanceID, accessTokenID).Execute() | ||
| if err != nil { | ||
| tfutils.LogError( | ||
| ctx, | ||
| &resp.Diagnostics, | ||
| err, | ||
| "Reading TelemetryRouter access token", | ||
| fmt.Sprintf("Access token with ID %q does not exist in project %q.", accessTokenID, projectID), | ||
| map[int]string{ | ||
| http.StatusForbidden: fmt.Sprintf("Project with ID %q not found or forbidden access", projectID), | ||
| }, | ||
| ) | ||
| resp.State.RemoveResource(ctx) | ||
| return | ||
| } | ||
| ctx = core.LogResponse(ctx) | ||
|
|
||
| err = mapDataSourceFields(ctx, accessTokenResponse, &model) | ||
| if err != nil { | ||
| core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading TelemetryRouter access token", 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, "TelemetryRouter access token read", map[string]interface{}{ | ||
| "access_token_id": accessTokenID, | ||
| }) | ||
| } | ||
|
|
||
| func mapDataSourceFields(_ context.Context, accessToken *telemetryrouter.GetAccessTokenResponse, model *DataSourceModel) error { | ||
| if accessToken == nil { | ||
| return fmt.Errorf("access token is nil") | ||
| } | ||
| if model == nil { | ||
| return fmt.Errorf("model is nil") | ||
| } | ||
|
|
||
| var accessTokenID string | ||
| if model.AccessTokenID.ValueString() != "" { | ||
| accessTokenID = model.AccessTokenID.ValueString() | ||
| } else if accessToken.Id != "" { | ||
| accessTokenID = accessToken.Id | ||
| } else { | ||
| return fmt.Errorf("access token id not present") | ||
| } | ||
|
|
||
| model.ID = tfutils.BuildInternalTerraformId(model.ProjectID.ValueString(), model.Region.ValueString(), model.InstanceID.ValueString(), accessTokenID) | ||
| model.AccessTokenID = types.StringValue(accessTokenID) | ||
| model.Region = types.StringValue(model.Region.ValueString()) | ||
| model.CreatorID = types.StringValue(accessToken.CreatorId) | ||
| if accessToken.Description != nil && *accessToken.Description != "" { | ||
| model.Description = types.StringPointerValue(accessToken.Description) | ||
| } | ||
| model.DisplayName = types.StringValue(accessToken.DisplayName) | ||
| model.Status = types.StringValue(accessToken.Status) | ||
|
|
||
| model.ExpirationTime = types.StringNull() | ||
| if accessToken.HasExpirationTime() && accessToken.ExpirationTime.Get() != nil { | ||
| model.ExpirationTime = types.StringValue(accessToken.ExpirationTime.Get().Format(time.RFC3339)) | ||
| } | ||
|
|
||
| return nil | ||
| } |
94 changes: 94 additions & 0 deletions
94
stackit/internal/services/telemetryrouter/accesstoken/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,94 @@ | ||
| package accesstoken | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/google/go-cmp/cmp" | ||
| "github.com/hashicorp/terraform-plugin-framework/types" | ||
| "github.com/stackitcloud/stackit-sdk-go/core/utils" | ||
| telemetryrouter "github.com/stackitcloud/stackit-sdk-go/services/telemetryrouter/v1betaapi" | ||
| ) | ||
|
|
||
| func fixtureDataSourceModel(mods ...func(model *DataSourceModel)) *DataSourceModel { | ||
| model := &DataSourceModel{ | ||
| ID: types.StringValue("pid,rid,iid,atid"), | ||
| AccessTokenID: types.StringValue("atid"), | ||
| InstanceID: types.StringValue("iid"), | ||
| Region: types.StringValue("rid"), | ||
| ProjectID: types.StringValue("pid"), | ||
| CreatorID: types.StringValue(""), | ||
| Description: types.String{}, | ||
| DisplayName: types.StringValue(""), | ||
| ExpirationTime: types.String{}, | ||
| Status: types.StringValue("active"), | ||
| } | ||
| for _, mod := range mods { | ||
| mod(model) | ||
| } | ||
| return model | ||
| } | ||
|
|
||
| func TestMapDataSourceFields(t *testing.T) { | ||
| tests := []struct { | ||
| description string | ||
| input *telemetryrouter.GetAccessTokenResponse | ||
| expected *DataSourceModel | ||
| wantErr bool | ||
| }{ | ||
| { | ||
| description: "min values", | ||
| input: fixtureGetAccessToken(), | ||
| expected: fixtureDataSourceModel(), | ||
| }, | ||
| { | ||
| description: "max values", | ||
| input: fixtureGetAccessToken(func(accessToken *telemetryrouter.GetAccessTokenResponse) { | ||
| accessToken.Description = utils.Ptr("description") | ||
| accessToken.DisplayName = "display-name" | ||
| accessToken.CreatorId = "testUser" | ||
| accessToken.ExpirationTime = *telemetryrouter.NewNullableTime(&testTime) | ||
| }), | ||
| expected: fixtureDataSourceModel(func(model *DataSourceModel) { | ||
| model.Description = types.StringValue("description") | ||
| model.DisplayName = types.StringValue("display-name") | ||
| model.CreatorID = types.StringValue("testUser") | ||
| model.ExpirationTime = types.StringValue(testTime.Format(time.RFC3339)) | ||
| }), | ||
| }, | ||
| { | ||
| description: "nil input", | ||
| wantErr: true, | ||
| expected: fixtureDataSourceModel(), | ||
| }, | ||
| { | ||
| description: "nil access token id", | ||
| input: &telemetryrouter.GetAccessTokenResponse{}, | ||
| wantErr: true, | ||
| expected: fixtureDataSourceModel(), | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.description, func(t *testing.T) { | ||
| state := &DataSourceModel{ | ||
| ProjectID: tt.expected.ProjectID, | ||
| Region: tt.expected.Region, | ||
| InstanceID: tt.expected.InstanceID, | ||
| } | ||
| 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.