-
Notifications
You must be signed in to change notification settings - Fork 67
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
feat: migrate users data source to tf framework #795
Merged
+360
−79
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
935b2f0
Add users datasource in framework
N-lson cccf90d
Replace users datasource with framework version
N-lson 223641e
Remove password, make username not sensitive
N-lson db93e18
Remove comment
N-lson 233ef54
Update descriptions and generated docs
N-lson 8091a01
Migrate users datasource test
N-lson db719e4
Remove users datasource in SDK
N-lson ff3f422
Add deprecation message to space id
N-lson 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 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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think
space_id
should be removed entirely since users aren't space-scoped but I've left it in for backwards compatibility.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm, good point. Lets mark it as deprecated.