Skip to content
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(org): add datasource cloudvanue_org_group #97

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions docs/data-sources/org_group.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "cloudavenue_org_group Data Source - cloudavenue"
subcategory: ""
description: |-
Provides a CloudAvenue Org User data source. This can be used to read group.
---

# cloudavenue_org_group (Data Source)

Provides a CloudAvenue Org User data source. This can be used to read group.

## Example Usage

```terraform
data "cloudavenue_org_group" "example" {
name = "your_value"
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `name` (String) A name for the org group

### Optional

- `description` (String) Description of the org group

### Read-Only

- `id` (String) The ID is a group `name`.
- `role` (String) The role to assign to the org group
- `user_names` (List of String) Set of user names that belong to the org group


3 changes: 3 additions & 0 deletions examples/data-sources/cloudavenue_org_group/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
data "cloudavenue_org_group" "example" {
name = "your_value"
}
132 changes: 132 additions & 0 deletions internal/provider/org/group_datasource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
// Package org provides a Terraform datasource.
package org

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"

"github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/client"
)

var (
_ datasource.DataSource = &orgGroupDataSource{}
_ datasource.DataSourceWithConfigure = &orgGroupDataSource{}
)

func NewOrgGroupDataSource() datasource.DataSource {
return &orgGroupDataSource{}
}

type orgGroupDataSource struct {
client *client.CloudAvenue
}

type orgGroupDataSourceModel struct {
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Description types.String `tfsdk:"description"`
Role types.String `tfsdk:"role"`
UserNames types.List `tfsdk:"user_names"`
}

func (d *orgGroupDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_" + "org_group"
}

func (d *orgGroupDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Provides a CloudAvenue Org User data source. This can be used to read group.",

Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Computed: true,
MarkdownDescription: "The ID is a group `name`.",
},
"name": schema.StringAttribute{
Required: true,
MarkdownDescription: "A name for the org group",
},
"description": schema.StringAttribute{
Optional: true,
MarkdownDescription: "Description of the org group",
},
"role": schema.StringAttribute{
Computed: true,
MarkdownDescription: "The role to assign to the org group",
},
"user_names": schema.ListAttribute{
ElementType: types.StringType,
Computed: true,
MarkdownDescription: "Set of user names that belong to the org group",
},
},
}
}

func (d *orgGroupDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
// Prevent panic if the provider has not been configured.
if req.ProviderData == nil {
return
}

client, ok := req.ProviderData.(*client.CloudAvenue)

if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected *client.CloudAvenue, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)

return
}

d.client = client
}

func (d *orgGroupDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data orgGroupDataSourceModel

// Read Terraform configuration data into the model
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)

if resp.Diagnostics.HasError() {
return
}

// group creation is accessible only for administator account
adminOrg, err := d.client.Vmware.GetAdminOrgByNameOrId(d.client.GetOrg())
if err != nil {
resp.Diagnostics.AddError("Error retrieving Org", err.Error())
return
}

orgGroup, err := adminOrg.GetGroupByName(data.Name.ValueString(), false)
if err != nil {
resp.Diagnostics.AddError("Error retrieving Org Group", err.Error())
return
}

var userNames []attr.Value
for _, user := range orgGroup.Group.UsersList.UserReference {
userNames = append(userNames, types.StringValue(user.Name))
}

data = orgGroupDataSourceModel{
ID: types.StringValue(orgGroup.Group.ID),
Name: types.StringValue(orgGroup.Group.Name),
Description: types.StringValue(orgGroup.Group.Description),
Role: types.StringValue(orgGroup.Group.Role.Name),
UserNames: types.ListValueMust(types.StringType, userNames),
}

// Save data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
}
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func (p *cloudavenueProvider) DataSources(_ context.Context) []func() datasource
vapp.NewVappDataSource,
catalog.NewCatalogDataSource,
org.NewOrgUserDataSource,
org.NewOrgGroupDataSource,
catalog.NewCatalogsDataSource,
}
}
Expand Down
42 changes: 42 additions & 0 deletions internal/tests/org_group_datasource_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package tests

import (
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

//go:generate go run github.com/FrangipaneTeam/tf-doc-extractor@latest -filename $GOFILE -example-dir ../../examples -test
const testAccOrgGroupDataSourceConfig = `
resource "cloudavenue_org_group" "example" {
name = "OrgTest"
role = "Organization Administrator"
description = "org test from go test"
}

data "cloudavenue_org_group" "example" {
name = cloudavenue_org_group.example.name
}
`

func TestAccOrgGroupDataSource(t *testing.T) {
resourceName := "data.cloudavenue_org_group.example"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
// Read testing
{
Config: testAccOrgGroupDataSourceConfig,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet(resourceName, "id"),
resource.TestCheckResourceAttr(resourceName, "name", "OrgTest"),
resource.TestCheckResourceAttr(resourceName, "role", "Organization Administrator"),
resource.TestCheckResourceAttr(resourceName, "description", "org test from go test"),
resource.TestCheckResourceAttr(resourceName, "user_names.#", "0"),
),
},
},
})
}