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 #900

Merged
merged 1 commit into from
Dec 18, 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/892.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_group` - Added a new data source to retrieve information about a Service Engine Group.
```
39 changes: 39 additions & 0 deletions docs/data-sources/alb_service_engine_group.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
page_title: "cloudavenue_alb_service_engine_group Data Source - cloudavenue"
subcategory: "ALB (Advanced Load Balancer)"
description: |-
The cloudavenue_alb_service_engine_group data source allows you to retrieve information about an ALB Service Engine Group.
---

# cloudavenue_alb_service_engine_group (Data Source)

The `cloudavenue_alb_service_engine_group` data source allows you to retrieve information about an ALB Service Engine Group.

## Example Usage

```terraform
data "cloudavenue_alb_service_engine_group" "example" {
name = "my-service-engine"
edge_gateway_name = data.cloudavenue_edge_gateway.example.name
}

output "example" {
value = data.cloudavenue_alb_service_engine_group.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`.
- `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,8 @@
data "cloudavenue_alb_service_engine_group" "example" {
name = "my-service-engine"
edge_gateway_name = data.cloudavenue_edge_gateway.example.name
}

output "example" {
value = data.cloudavenue_alb_service_engine_group.example
}
127 changes: 127 additions & 0 deletions internal/provider/alb/service_engine_group_datasource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
// 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"
)

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

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

type albServiceEngineGroupDataSource 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) {
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 *albServiceEngineGroupDataSource) 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 *albServiceEngineGroupDataSource) 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 *albServiceEngineGroupDataSource) 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{}

// 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
albSEG *v1.EdgeGatewayALBServiceEngineGroupModel
)
if config.ID.IsKnown() {
albSEG, err = d.edgegw.GetALBServiceEngineGroup(config.ID.Get())
} else {
albSEG, err = d.edgegw.GetALBServiceEngineGroup(config.Name.Get())
}
if err != nil {
resp.Diagnostics.AddError("Error retrieving Service Engine Group", err.Error())
return
}

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)

// Set state
resp.Diagnostics.Append(resp.State.Set(ctx, config)...)
}
82 changes: 82 additions & 0 deletions internal/provider/alb/service_engine_group_schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package alb

import (
"context"

"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"

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

"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"

superschema "github.com/FrangipaneTeam/terraform-plugin-framework-superschema"
)

func albServiceEngineGroupSchema(_ 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.",
},
Attributes: map[string]superschema.Attribute{
"id": superschema.SuperStringAttribute{
DataSource: &schemaD.StringAttribute{
Computed: true,
Optional: true,
MarkdownDescription: "The ID of the ALB Service Engine Group.",
Validators: []validator.String{
stringvalidator.ExactlyOneOf(path.MatchRoot("id"), path.MatchRoot("name")),
},
},
},
"name": superschema.SuperStringAttribute{
DataSource: &schemaD.StringAttribute{
MarkdownDescription: "The name of the ALB Service Engine Group.",
Computed: true,
Optional: true,
Validators: []validator.String{
stringvalidator.ExactlyOneOf(path.MatchRoot("id"), path.MatchRoot("name")),
},
},
},
"edge_gateway_id": superschema.SuperStringAttribute{
DataSource: &schemaD.StringAttribute{
MarkdownDescription: "Edge gateway ID in which ALB Service Engine Group should be located.",
Optional: true,
Validators: []validator.String{
stringvalidator.ExactlyOneOf(path.MatchRoot("edge_gateway_id"), path.MatchRoot("edge_gateway_name")),
},
Computed: true,
},
},
"edge_gateway_name": superschema.SuperStringAttribute{
DataSource: &schemaD.StringAttribute{
MarkdownDescription: "Edge gateway Name in which ALB Service Engine Group should be located.",
Optional: true,
Validators: []validator.String{
stringvalidator.ExactlyOneOf(path.MatchRoot("edge_gateway_id"), path.MatchRoot("edge_gateway_name")),
},
Computed: true,
},
},
"max_virtual_services": superschema.SuperInt64Attribute{
DataSource: &schemaD.Int64Attribute{
Computed: true,
MarkdownDescription: "The maximum number of virtual services that can be deployed on the ALB Service Engine Group.",
},
},
"reserved_virtual_services": superschema.SuperInt64Attribute{
DataSource: &schemaD.Int64Attribute{
Computed: true,
MarkdownDescription: "The number of reserved virtual services for the ALB Service Engine Group.",
},
},
"deployed_virtual_services": superschema.SuperInt64Attribute{
DataSource: &schemaD.Int64Attribute{
Computed: true,
MarkdownDescription: "The number of deployed virtual services on the ALB Service Engine Group.",
},
},
},
}
}
57 changes: 57 additions & 0 deletions internal/provider/alb/service_engine_group_schema_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package alb_test

import (
"context"
"testing"

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

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

// TODO : Comment or uncomment the following imports if you are using resources or/and datasources
/*
// Unit test for the schema of the resource cloudavenue_alb_AlbServiceEngineGroupDatasourceName
func TestAlbServiceEngineGroupDatasourceNameResourceSchema(t *testing.T) {
t.Parallel()

ctx := context.Background()
schemaResponse := &fwresource.SchemaResponse{}

// Instantiate the resource.Resource and call its Schema method
alb.NewAlbServiceEngineGroupDatasourceNameResource().Schema(ctx, fwresource.SchemaRequest{}, schemaResponse)

if schemaResponse.Diagnostics.HasError() {
t.Fatalf("Schema method diagnostics: %+v", schemaResponse.Diagnostics)
}

// Validate the schema
diagnostics := schemaResponse.Schema.ValidateImplementation(ctx)

if diagnostics.HasError() {
t.Fatalf("Schema validation diagnostics: %+v", diagnostics)
}
}
*/
// Unit test for the schema of the datasource cloudavenue_alb_AlbServiceEngineGroupDatasourceName

func TestALBServiceEngineGroupDataSourceSchema(t *testing.T) {
t.Parallel()

ctx := context.Background()
schemaResponse := &fwdatasource.SchemaResponse{}

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

if schemaResponse.Diagnostics.HasError() {
t.Fatalf("Schema method diagnostics: %+v", schemaResponse.Diagnostics)
}

// Validate the schema
diagnostics := schemaResponse.Schema.ValidateImplementation(ctx)

if diagnostics.HasError() {
t.Fatalf("Schema validation diagnostics: %+v", diagnostics)
}
}
15 changes: 15 additions & 0 deletions internal/provider/alb/service_engine_group_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package alb

import (
supertypes "github.com/FrangipaneTeam/terraform-plugin-framework-supertypes"
)

type albServiceEngineGroupModel struct {
ID supertypes.StringValue `tfsdk:"id"`
Name supertypes.StringValue `tfsdk:"name"`
EdgeGatewayID supertypes.StringValue `tfsdk:"edge_gateway_id"`
EdgeGatewayName supertypes.StringValue `tfsdk:"edge_gateway_name"`
MaxVirtualServices supertypes.Int64Value `tfsdk:"max_virtual_services"`
ReservedVirtualServices supertypes.Int64Value `tfsdk:"reserved_virtual_services"`
DeployedVirtualServices supertypes.Int64Value `tfsdk:"deployed_virtual_services"`
}
1 change: 1 addition & 0 deletions internal/provider/provider_datasources.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func (p *cloudavenueProvider) DataSources(_ context.Context) []func() datasource
return []func() datasource.DataSource{
// * ALB
alb.NewAlbPoolDataSource,
alb.NewALBServiceEngineGroupDataSource,

// * TIER0
vrf.NewTier0VrfsDataSource,
Expand Down
Loading
Loading