diff --git a/docs/data-sources/role.md b/docs/data-sources/role.md new file mode 100644 index 0000000..6afe52e --- /dev/null +++ b/docs/data-sources/role.md @@ -0,0 +1,30 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "sonatypeiq_role Data Source - terraform-provider-sonatypeiq" +subcategory: "" +description: |- + Use this data source to get a role +--- + +# sonatypeiq_role (Data Source) + +Use this data source to get a role + +## Example Usage + +```terraform +data "sonatypeiq_role" "developer" { + name = "Developer" +} +``` + + +## Schema + +### Required + +- `name` (String) The role name + +### Read-Only + +- `id` (String) The ID of this resource. diff --git a/examples/data-sources/sonatypeiq_role/data-source.tf b/examples/data-sources/sonatypeiq_role/data-source.tf new file mode 100644 index 0000000..314012b --- /dev/null +++ b/examples/data-sources/sonatypeiq_role/data-source.tf @@ -0,0 +1,3 @@ +data "sonatypeiq_role" "developer" { + name = "Developer" +} diff --git a/go.mod b/go.mod index 751b957..84291ee 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.21 require ( github.com/hashicorp/terraform-plugin-framework v1.4.2 + github.com/hashicorp/terraform-plugin-framework-validators v0.12.0 github.com/hashicorp/terraform-plugin-go v0.22.0 github.com/hashicorp/terraform-plugin-log v0.9.0 github.com/hashicorp/terraform-plugin-sdk v1.17.2 diff --git a/go.sum b/go.sum index 0471f27..8a5a242 100644 --- a/go.sum +++ b/go.sum @@ -243,6 +243,8 @@ github.com/hashicorp/terraform-plugin-docs v0.18.0 h1:2bINhzXc+yDeAcafurshCrIjtd github.com/hashicorp/terraform-plugin-docs v0.18.0/go.mod h1:iIUfaJpdUmpi+rI42Kgq+63jAjI8aZVTyxp3Bvk9Hg8= github.com/hashicorp/terraform-plugin-framework v1.4.2 h1:P7a7VP1GZbjc4rv921Xy5OckzhoiO3ig6SGxwelD2sI= github.com/hashicorp/terraform-plugin-framework v1.4.2/go.mod h1:GWl3InPFZi2wVQmdVnINPKys09s9mLmTZr95/ngLnbY= +github.com/hashicorp/terraform-plugin-framework-validators v0.12.0 h1:HOjBuMbOEzl7snOdOoUfE2Jgeto6JOjLVQ39Ls2nksc= +github.com/hashicorp/terraform-plugin-framework-validators v0.12.0/go.mod h1:jfHGE/gzjxYz6XoUwi/aYiiKrJDeutQNUtGQXkaHklg= github.com/hashicorp/terraform-plugin-go v0.22.0 h1:1OS1Jk5mO0f5hrziWJGXXIxBrMe2j/B8E+DVGw43Xmc= github.com/hashicorp/terraform-plugin-go v0.22.0/go.mod h1:mPULV91VKss7sik6KFEcEu7HuTogMLLO/EvWCuFkRVE= github.com/hashicorp/terraform-plugin-log v0.9.0 h1:i7hOA+vdAItN1/7UrfBqBwvYPQ9TFvymaRGZED3FCV0= diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 7f1b1c9..2a778cf 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -180,6 +180,7 @@ func (p *SonatypeIqProvider) DataSources(ctx context.Context) []func() datasourc OrganizationDataSource, OrganizationsDataSource, SystemConfigDataSource, + RoleDataSource, } } diff --git a/internal/provider/role_data_source.go b/internal/provider/role_data_source.go new file mode 100644 index 0000000..6c1c9be --- /dev/null +++ b/internal/provider/role_data_source.go @@ -0,0 +1,110 @@ +/* + * Copyright (c) 2019-present Sonatype, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package provider + +import ( + "context" + "fmt" + + "github.com/hashicorp/terraform-plugin-framework/datasource" + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" + "github.com/hashicorp/terraform-plugin-framework/types" + + sonatypeiq "github.com/sonatype-nexus-community/nexus-iq-api-client-go" +) + +// Ensure the implementation satisfies the expected interfaces. +var ( + _ datasource.DataSource = &roleDataSource{} + _ datasource.DataSourceWithConfigure = &roleDataSource{} +) + +// RoleDataSource is a helper function to simplify the provider implementation. +func RoleDataSource() datasource.DataSource { + return &roleDataSource{} +} + +// roleDataSource is the data source implementation. +type roleDataSource struct { + baseDataSource +} + +type roleDataSourceModel struct { + ID types.String `tfsdk:"id"` + Name types.String `tfsdk:"name"` +} + +// Metadata returns the data source type name. +func (d *roleDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { + resp.TypeName = req.ProviderTypeName + "_role" +} + +// Schema defines the schema for the data source. +func (d *roleDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { + resp.Schema = schema.Schema{ + Description: "Use this data source to get a role", + Attributes: map[string]schema.Attribute{ + "id": schema.StringAttribute{ + Computed: true, + }, + "name": schema.StringAttribute{ + Description: "The role name", + Required: true, + }, + }, + } +} + +// Read refreshes the Terraform state with the latest data. +func (d *roleDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { + var data roleDataSourceModel + + // Read Terraform configuration data into the model + resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) + + ctx = context.WithValue( + ctx, + sonatypeiq.ContextBasicAuth, + d.auth, + ) + + roleList, _, err := d.client.RolesAPI.GetRoles(ctx).Execute() + if err != nil { + resp.Diagnostics.AddError( + "Unable to Read IQ Roles", + err.Error(), + ) + return + } + + for _, role := range roleList.Roles { + if role.GetName() == data.Name.ValueString() { + data.ID = types.StringValue(role.GetId()) + } + } + + if data.ID.IsNull() { + resp.Diagnostics.AddError( + "Unable to find role", + fmt.Sprintf("Role '%s' does not exist", data.Name.ValueString()), + ) + return + } + + // Save data into Terraform state + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) +} diff --git a/internal/provider/role_data_source_test.go b/internal/provider/role_data_source_test.go new file mode 100644 index 0000000..f3159ba --- /dev/null +++ b/internal/provider/role_data_source_test.go @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2019-present Sonatype, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package provider + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-testing/helper/resource" +) + +func TestAccRoleDataSource(t *testing.T) { + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + // Read testing + { + Config: providerConfig + `data "sonatypeiq_role" "role_by_name" { + name = "Developer" + }`, + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr("data.sonatypeiq_role.role_by_name", "name", "Developer"), + resource.TestCheckResourceAttr("data.sonatypeiq_role.role_by_name", "id", "1da70fae1fd54d6cb7999871ebdb9a36"), + ), + }, + }, + }) +}