Skip to content

Commit

Permalink
feat: add role data source (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
iverberk authored Apr 8, 2024
1 parent 6a1e639 commit a75d10a
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 0 deletions.
30 changes: 30 additions & 0 deletions docs/data-sources/role.md
Original file line number Diff line number Diff line change
@@ -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 generated by tfplugindocs -->
## Schema

### Required

- `name` (String) The role name

### Read-Only

- `id` (String) The ID of this resource.
3 changes: 3 additions & 0 deletions examples/data-sources/sonatypeiq_role/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
data "sonatypeiq_role" "developer" {
name = "Developer"
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ func (p *SonatypeIqProvider) DataSources(ctx context.Context) []func() datasourc
OrganizationDataSource,
OrganizationsDataSource,
SystemConfigDataSource,
RoleDataSource,
}
}

Expand Down
110 changes: 110 additions & 0 deletions internal/provider/role_data_source.go
Original file line number Diff line number Diff line change
@@ -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)...)
}
41 changes: 41 additions & 0 deletions internal/provider/role_data_source_test.go
Original file line number Diff line number Diff line change
@@ -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"),
),
},
},
})
}

0 comments on commit a75d10a

Please sign in to comment.