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: Add ALB Service Engine Group datasource #911

Merged
merged 1 commit into from
Dec 23, 2024
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
3 changes: 3 additions & 0 deletions .changelog/901.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-data-source
`datasource/cloudavenue_alb_service_engine_groups` - Added a new data source to list all Service Engine Group attached to an Edge Gateway.
```
51 changes: 51 additions & 0 deletions docs/data-sources/alb_service_engine_groups.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
page_title: "cloudavenue_alb_service_engine_groups Data Source - cloudavenue"
subcategory: "ALB (Advanced Load Balancer)"
description: |-
The cloudavenue_alb_service_engine_groups data source allows you to retrieve information about all the Service Engine Group of an Edge Gateway.
---

# cloudavenue_alb_service_engine_groups (Data Source)

The `cloudavenue_alb_service_engine_groups` data source allows you to retrieve information about all the Service Engine Group of an Edge Gateway.

## Example Usage

```terraform
data "cloudavenue_alb_service_engine_groups" "example" {
edge_gateway_name = data.cloudavenue_edge_gateway.example.name
}

output "example" {
value = data.cloudavenue_alb_service_engine_groups.example
}
```

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

### Optional

- `edge_gateway_id` (String) Edge gateway ID in which ALB Service Engine Group should be located. Ensure that one and only one attribute from this collection is set : `edge_gateway_id`, `edge_gateway_name`.
- `edge_gateway_name` (String) Edge gateway Name in which ALB Service Engine Group should be located. Ensure that one and only one attribute from this collection is set : `edge_gateway_id`, `edge_gateway_name`.

### Read-Only

- `id` (String) The ID of the service engine groups.
- `service_engine_groups` (Attributes List) The list of service engine groups. (see [below for nested schema](#nestedatt--service_engine_groups))

<a id="nestedatt--service_engine_groups"></a>
### Nested Schema for `service_engine_groups`

Optional:

- `edge_gateway_id` (String) Edge gateway ID in which ALB Service Engine Group should be located. Ensure that one and only one attribute from this collection is set : `edge_gateway_id`, `edge_gateway_name`.
- `edge_gateway_name` (String) Edge gateway Name in which ALB Service Engine Group should be located. Ensure that one and only one attribute from this collection is set : `edge_gateway_id`, `edge_gateway_name`.
- `id` (String) The ID of the ALB Service Engine Group. Ensure that one and only one attribute from this collection is set : `id`, `name`.
- `name` (String) The name of the ALB Service Engine Group. Ensure that one and only one attribute from this collection is set : `id`, `name`.

Read-Only:

- `deployed_virtual_services` (Number) The number of deployed virtual services on the ALB Service Engine Group.
- `max_virtual_services` (Number) The maximum number of virtual services that can be deployed on the ALB Service Engine Group.
- `reserved_virtual_services` (Number) The number of reserved virtual services for the ALB Service Engine Group.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
data "cloudavenue_alb_service_engine_groups" "example" {
edge_gateway_name = data.cloudavenue_edge_gateway.example.name
}

output "example" {
value = data.cloudavenue_alb_service_engine_groups.example
}
74 changes: 48 additions & 26 deletions internal/provider/alb/service_engine_group_datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ import (
"context"
"fmt"

"github.com/vmware/go-vcloud-director/v2/govcd"

"github.com/hashicorp/terraform-plugin-framework/diag"

"github.com/hashicorp/terraform-plugin-framework/datasource"

commoncloudavenue "github.com/orange-cloudavenue/cloudavenue-sdk-go/pkg/common/cloudavenue"
v1 "github.com/orange-cloudavenue/cloudavenue-sdk-go/v1"
"github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/client"
"github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/metrics"
Expand All @@ -17,22 +20,22 @@ import (
)

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

func NewALBServiceEngineGroupDataSource() datasource.DataSource {
return &albServiceEngineGroupDataSource{}
func NewServiceEngineGroupDataSource() datasource.DataSource {
return &serviceEngineGroupDataSource{}
}

type albServiceEngineGroupDataSource struct {
type serviceEngineGroupDataSource struct {
client *client.CloudAvenue
edgegw edgegw.EdgeGateway
org org.Org
}

// Init Initializes the data source.
func (d *albServiceEngineGroupDataSource) Init(ctx context.Context, dm *albServiceEngineGroupModel) (diags diag.Diagnostics) {
func (d *serviceEngineGroupDataSource) Init(ctx context.Context, dm *serviceEngineGroupModel) (diags diag.Diagnostics) {
var err error

d.org, diags = org.Init(d.client)
Expand All @@ -53,15 +56,15 @@ func (d *albServiceEngineGroupDataSource) Init(ctx context.Context, dm *albServi
return
}

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

func (d *albServiceEngineGroupDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = albServiceEngineGroupSchema(ctx).GetDataSource(ctx)
func (d *serviceEngineGroupDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = serviceEngineGroupSchema(ctx).GetDataSource(ctx)
}

func (d *albServiceEngineGroupDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
func (d *serviceEngineGroupDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
// Prevent panic if the provider has not been configured.
if req.ProviderData == nil {
return
Expand All @@ -78,10 +81,10 @@ func (d *albServiceEngineGroupDataSource) Configure(ctx context.Context, req dat
d.client = client
}

func (d *albServiceEngineGroupDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
func (d *serviceEngineGroupDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
defer metrics.New("data.cloudavenue_alb_service_engine_group", d.client.GetOrgName(), metrics.Read)()

config := &albServiceEngineGroupModel{}
config := &serviceEngineGroupModel{}

// Read Terraform configuration data into the model
resp.Diagnostics.Append(req.Config.Get(ctx, config)...)
Expand All @@ -99,29 +102,48 @@ func (d *albServiceEngineGroupDataSource) Read(ctx context.Context, req datasour
Implement the data source read logic here.
*/

// Read data from the API
data, found, diags := d.read(config)
if !found {
diags.AddError("Error Not Found", "The Service Engine Group was not found")
}
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

// Set state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}

func (d *serviceEngineGroupDataSource) read(dm *serviceEngineGroupModel) (data *serviceEngineGroupModel, found bool, diags diag.Diagnostics) {
data = &serviceEngineGroupModel{}

// Get ServiceEngineGroup
var (
err error
albSEG *v1.EdgeGatewayALBServiceEngineGroupModel
)
if config.ID.IsKnown() {
albSEG, err = d.edgegw.GetALBServiceEngineGroup(config.ID.Get())
if dm.ID.IsKnown() {
albSEG, err = d.edgegw.GetALBServiceEngineGroup(dm.ID.Get())
} else {
albSEG, err = d.edgegw.GetALBServiceEngineGroup(config.Name.Get())
albSEG, err = d.edgegw.GetALBServiceEngineGroup(dm.Name.Get())
}
if err != nil {
resp.Diagnostics.AddError("Error retrieving Service Engine Group", err.Error())
return
if commoncloudavenue.IsNotFound(err) || govcd.IsNotFound(err) {
return nil, false, diags
}
diags.AddError("Error retrieving Service Engine Group", err.Error())
return nil, true, diags
}

config.ID.Set(albSEG.ID)
config.Name.Set(albSEG.Name)
config.EdgeGatewayID.Set(albSEG.GatewayRef.ID)
config.EdgeGatewayName.Set(albSEG.GatewayRef.Name)
config.MaxVirtualServices.SetIntPtr(albSEG.MaxVirtualServices)
config.ReservedVirtualServices.SetIntPtr(albSEG.MinVirtualServices)
config.DeployedVirtualServices.SetInt(albSEG.NumDeployedVirtualServices)
data.ID.Set(albSEG.ID)
data.Name.Set(albSEG.Name)
data.EdgeGatewayID.Set(albSEG.GatewayRef.ID)
data.EdgeGatewayName.Set(albSEG.GatewayRef.Name)
data.MaxVirtualServices.SetIntPtr(albSEG.MaxVirtualServices)
data.ReservedVirtualServices.SetIntPtr(albSEG.MinVirtualServices)
data.DeployedVirtualServices.SetInt(albSEG.NumDeployedVirtualServices)

// Set state
resp.Diagnostics.Append(resp.State.Set(ctx, config)...)
return data, true, diags
}
2 changes: 1 addition & 1 deletion internal/provider/alb/service_engine_group_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
superschema "github.com/FrangipaneTeam/terraform-plugin-framework-superschema"
)

func albServiceEngineGroupSchema(_ context.Context) superschema.Schema {
func serviceEngineGroupSchema(_ context.Context) superschema.Schema {
return superschema.Schema{
DataSource: superschema.SchemaDetails{
MarkdownDescription: "The `cloudavenue_alb_service_engine_group` data source allows you to retrieve information about an ALB Service Engine Group.",
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/alb/service_engine_group_schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestALBServiceEngineGroupDataSourceSchema(t *testing.T) {
schemaResponse := &fwdatasource.SchemaResponse{}

// Instantiate the datasource.Datasource and call its Schema method
alb.NewALBServiceEngineGroupDataSource().Schema(ctx, fwdatasource.SchemaRequest{}, schemaResponse)
alb.NewServiceEngineGroupDataSource().Schema(ctx, fwdatasource.SchemaRequest{}, schemaResponse)

if schemaResponse.Diagnostics.HasError() {
t.Fatalf("Schema method diagnostics: %+v", schemaResponse.Diagnostics)
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/alb/service_engine_group_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
supertypes "github.com/FrangipaneTeam/terraform-plugin-framework-supertypes"
)

type albServiceEngineGroupModel struct {
type serviceEngineGroupModel struct {
ID supertypes.StringValue `tfsdk:"id"`
Name supertypes.StringValue `tfsdk:"name"`
EdgeGatewayID supertypes.StringValue `tfsdk:"edge_gateway_id"`
Expand Down
141 changes: 141 additions & 0 deletions internal/provider/alb/service_engine_groups_datasource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
// Package alb provides a Terraform datasource.
package alb

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/diag"

"github.com/hashicorp/terraform-plugin-framework/datasource"

v1 "github.com/orange-cloudavenue/cloudavenue-sdk-go/v1"
"github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/client"
"github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/metrics"
"github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/provider/common/edgegw"
"github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/provider/common/org"
"github.com/orange-cloudavenue/terraform-provider-cloudavenue/pkg/utils"
)

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

func NewServiceEngineGroupsDataSource() datasource.DataSource {
return &serviceEngineGroupsDataSource{}
}

type serviceEngineGroupsDataSource struct {
client *client.CloudAvenue
edgegw edgegw.EdgeGateway
org org.Org
}

// Init Initializes the data source.
func (d *serviceEngineGroupsDataSource) Init(ctx context.Context, dm *serviceEngineGroupsModel) (diags diag.Diagnostics) {
var err error

d.org, diags = org.Init(d.client)
if diags.HasError() {
return
}

// Retrieve VDC from edge gateway
d.edgegw, err = d.org.GetEdgeGateway(edgegw.BaseEdgeGW{
ID: dm.EdgeGatewayID.StringValue,
Name: dm.EdgeGatewayName.StringValue,
})
if err != nil {
diags.AddError("Error retrieving Edge Gateway", err.Error())
return
}

return
}

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

func (d *serviceEngineGroupsDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = serviceEngineGroupsSchema(ctx).GetDataSource(ctx)
}

func (d *serviceEngineGroupsDataSource) 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 *serviceEngineGroupsDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
defer metrics.New("data.cloudavenue_alb_service_engine_groups", d.client.GetOrgName(), metrics.Read)()

config := &serviceEngineGroupsModel{}

// Read Terraform configuration data into the model
resp.Diagnostics.Append(req.Config.Get(ctx, config)...)
if resp.Diagnostics.HasError() {
return
}

// Init the resource
resp.Diagnostics.Append(d.Init(ctx, config)...)
if resp.Diagnostics.HasError() {
return
}

/*
Implement the data source read logic here.
*/

// Get ServiceEngineGroup
var (
err error
albSEGs []*v1.EdgeGatewayALBServiceEngineGroupModel
)

// Get Service Engine Groups
albSEGs, err = d.edgegw.ListALBServiceEngineGroups()
if err != nil {
resp.Diagnostics.AddError("Error retrieving Service Engine Groups", err.Error())
return
}

// Set SDK model to Terraform model
seg := make([]*serviceEngineGroupModel, 0)
for _, albSEG := range albSEGs {
x := &serviceEngineGroupModel{}
x.ID.Set(albSEG.ID)
x.Name.Set(albSEG.Name)
x.EdgeGatewayID.Set(albSEG.GatewayRef.ID)
x.EdgeGatewayName.Set(albSEG.GatewayRef.Name)
x.MaxVirtualServices.SetIntPtr(albSEG.MaxVirtualServices)
x.ReservedVirtualServices.SetIntPtr(albSEG.MinVirtualServices)
x.DeployedVirtualServices.SetInt(albSEG.NumDeployedVirtualServices)
seg = append(seg, x)
}

// Set config
resp.Diagnostics.Append(config.ServiceEngineGroups.Set(ctx, seg)...)
if resp.Diagnostics.HasError() {
return
}
config.ID.Set(utils.GenerateUUID(fmt.Sprint(d.edgegw.EdgeName + d.edgegw.EdgeID)).ValueString())
config.EdgeGatewayID.Set(d.edgegw.EdgeGateway.ID)
config.EdgeGatewayName.Set(d.edgegw.EdgeGateway.Name)

// Set state
resp.Diagnostics.Append(resp.State.Set(ctx, config)...)
}
Loading
Loading