-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: migrate users data source to tf framework (#795)
* Add users datasource in framework * Replace users datasource with framework version * Remove password, make username not sensitive * Remove comment * Update descriptions and generated docs * Migrate users datasource test * Remove users datasource in SDK * Add deprecation message to space id
- Loading branch information
Showing
8 changed files
with
360 additions
and
79 deletions.
There are no files selected for viewing
This file contains 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 was deleted.
Oops, something went wrong.
This file contains 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 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,80 @@ | ||
package octopusdeploy_framework | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/users" | ||
"github.com/OctopusDeploy/terraform-provider-octopusdeploy/octopusdeploy_framework/schemas" | ||
"github.com/OctopusDeploy/terraform-provider-octopusdeploy/octopusdeploy_framework/util" | ||
"github.com/hashicorp/terraform-plugin-framework/datasource" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
"github.com/hashicorp/terraform-plugin-log/tflog" | ||
"time" | ||
) | ||
|
||
type userDataSource struct { | ||
*Config | ||
} | ||
|
||
type usersDataSourceModel struct { | ||
ID types.String `tfsdk:"id"` | ||
SpaceID types.String `tfsdk:"space_id"` | ||
IDs types.List `tfsdk:"ids"` | ||
Filter types.String `tfsdk:"filter"` | ||
Skip types.Int64 `tfsdk:"skip"` | ||
Take types.Int64 `tfsdk:"take"` | ||
Users types.List `tfsdk:"users"` | ||
} | ||
|
||
func NewUsersDataSource() datasource.DataSource { | ||
return &userDataSource{} | ||
} | ||
|
||
func (u *userDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { | ||
resp.TypeName = util.GetTypeName("users") | ||
} | ||
|
||
func (u *userDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { | ||
resp.Schema = schemas.UserSchema{}.GetDatasourceSchema() | ||
} | ||
|
||
func (u *userDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { | ||
u.Config = DataSourceConfiguration(req, resp) | ||
} | ||
|
||
func (u *userDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { | ||
var err error | ||
var data usersDataSourceModel | ||
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
query := users.UsersQuery{ | ||
IDs: util.GetIds(data.IDs), | ||
Filter: data.Filter.ValueString(), | ||
Skip: util.GetNumber(data.Skip), | ||
Take: util.GetNumber(data.Take), | ||
} | ||
|
||
util.DatasourceReading(ctx, "users", query) | ||
|
||
existingUsers, err := users.Get(u.Client, data.SpaceID.ValueString(), query) | ||
if err != nil { | ||
resp.Diagnostics.AddError("unable to load users", err.Error()) | ||
return | ||
} | ||
|
||
mappedUsers := []schemas.UserTypeResourceModel{} | ||
tflog.Debug(ctx, fmt.Sprintf("users returned from API: %#v", existingUsers)) | ||
for _, user := range existingUsers.Items { | ||
mappedUsers = append(mappedUsers, schemas.MapFromUser(user)) | ||
} | ||
|
||
util.DatasourceResultCount(ctx, "users", len(mappedUsers)) | ||
|
||
data.Users, _ = types.ListValueFrom(ctx, types.ObjectType{AttrTypes: schemas.UserObjectType()}, mappedUsers) | ||
data.ID = types.StringValue("Users " + time.Now().UTC().String()) | ||
|
||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) | ||
} |
17 changes: 8 additions & 9 deletions
17
octopusdeploy/data_source_users_test.go → ...deploy_framework/datasource_users_test.go
This file contains 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 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 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
Oops, something went wrong.