Skip to content

Commit

Permalink
feat: add broadcast_domain resource
Browse files Browse the repository at this point in the history
  • Loading branch information
acch committed Nov 5, 2024
1 parent 3db158a commit 23ad75f
Show file tree
Hide file tree
Showing 7 changed files with 453 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
resource "netapp-ontap_network_broadcast_domain" "example" {
# required to know which system to interface with
cx_profile_name = "svacluster"
ipspace = "Default"
mtu = 1500
name = "testme"
}
133 changes: 117 additions & 16 deletions internal/interfaces/networking_broadcast_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
)

// BroadcastDomainGetDataModelONTAP describes the GET record data model using go types for mapping.
// https://docs.netapp.com/us-en/ontap-restapi/ontap/get-network-ethernet-broadcast-domains-.html#response
type BroadcastDomainGetDataModelONTAP struct {
IPspace BroadcastDomainIPSpace `mapstructure:"ipspace"`
MTU int64 `mapstructure:"mtu"`
Expand All @@ -19,24 +20,25 @@ type BroadcastDomainGetDataModelONTAP struct {
}

// BroadcastDomainResourceBodyDataModelONTAP describes the body data model using go types for mapping.
// https://docs.netapp.com/us-en/ontap-restapi/ontap/post-network-ethernet-broadcast-domains.html#request-body
type BroadcastDomainResourceBodyDataModelONTAP struct {
IPspace BroadcastDomainIPSpace `mapstructure:"ipspace"`
MTU int64 `mapstructure:"mtu"`
Name string `mapstructure:"name"`
Ports []BroadcastDomainPort `mapstructure:"ports"`
UUID string `mapstructure:"uuid"`
IPspace BroadcastDomainIPSpace `mapstructure:"ipspace,omitempty"`
MTU int64 `mapstructure:"mtu,omitempty"`
Name string `mapstructure:"name,omitempty"`
Ports []BroadcastDomainPort `mapstructure:"ports,omitempty"`
UUID string `mapstructure:"uuid,omitempty"`
}

// BroadcastDomainIPSpace describes an IP space specifically for broadcast domains.
type BroadcastDomainIPSpace struct {
Name string `mapstructure:"name"`
UUID string `mapstructure:"uuid"`
Name string `mapstructure:"name,omitempty"`
// UUID string `mapstructure:"uuid,omitempty"`
}

// BroadcastDomainPort describes an ethernet port specifically for broadcast domains.
type BroadcastDomainPort struct {
Name string `mapstructure:"name"`
UUID string `mapstructure:"uuid"`
// UUID string `mapstructure:"uuid"`
}

// BroadcastDomainDataSourceFilterModel describes filter model.
Expand All @@ -45,7 +47,8 @@ type BroadcastDomainDataSourceFilterModel struct {
Name string `tfsdk:"name"`
}

// GetBroadcastDomain to get broadcast_domain info
// Retrieve broadcast domain details
// https://docs.netapp.com/us-en/ontap-restapi/ontap/get-network-ethernet-broadcast-domains-.html
func GetBroadcastDomain(errorHandler *utils.ErrorHandler, r restclient.RestClient, id string) (*BroadcastDomainGetDataModelONTAP, error) {
api := "/network/ethernet/broadcast-domains/" + id
query := r.NewQuery()
Expand Down Expand Up @@ -76,14 +79,18 @@ func GetBroadcastDomain(errorHandler *utils.ErrorHandler, r restclient.RestClien
)
}

tflog.Debug(errorHandler.Ctx, fmt.Sprintf("Read broadcast_domain data source: %#v", dataONTAP))
tflog.Debug(
errorHandler.Ctx,
fmt.Sprintf("Read broadcast_domain data source: %#v", dataONTAP),
)

return &dataONTAP, nil
}

// GetBroadcastDomainByName to get broadcast_domain info
// Retrieve broadcast domain details
// https://docs.netapp.com/us-en/ontap-restapi/ontap/get-network-ethernet-broadcast-domains.html
func GetBroadcastDomainByName(errorHandler *utils.ErrorHandler, r restclient.RestClient, ipspace, name string) (*BroadcastDomainGetDataModelONTAP, error) {
api := "/network/ethernet/broadcast-domains/"
api := "/network/ethernet/broadcast-domains"
query := r.NewQuery()
query.Set("ipspace", ipspace)
query.Set("name", name)
Expand Down Expand Up @@ -114,14 +121,18 @@ func GetBroadcastDomainByName(errorHandler *utils.ErrorHandler, r restclient.Res
)
}

tflog.Debug(errorHandler.Ctx, fmt.Sprintf("Read broadcast_domain data source: %#v", dataONTAP))
tflog.Debug(
errorHandler.Ctx,
fmt.Sprintf("Read broadcast_domain data source: %#v", dataONTAP),
)

return &dataONTAP, nil
}

// GetListBroadcastDomains to get broadcast_domain info for all resources matching a filter
// Retrieve broadcast domains for the entire cluster
// https://docs.netapp.com/us-en/ontap-restapi/ontap/get-network-ethernet-broadcast-domains.html
func GetListBroadcastDomains(errorHandler *utils.ErrorHandler, r restclient.RestClient, filter *BroadcastDomainDataSourceFilterModel) ([]BroadcastDomainGetDataModelONTAP, error) {
api := "network/ethernet/broadcast-domains/"
api := "network/ethernet/broadcast-domains"
query := r.NewQuery()
query.Fields([]string{
"ipspace",
Expand Down Expand Up @@ -163,7 +174,97 @@ func GetListBroadcastDomains(errorHandler *utils.ErrorHandler, r restclient.Rest
dataONTAP = append(dataONTAP, record)
}

tflog.Debug(errorHandler.Ctx, fmt.Sprintf("Read broadcast_domain data source: %#v", dataONTAP))
tflog.Debug(
errorHandler.Ctx,
fmt.Sprintf("Read broadcast_domain data source: %#v", dataONTAP),
)

return dataONTAP, nil
}

// Create a new broadcast domain
// https://docs.netapp.com/us-en/ontap-restapi/ontap/post-network-ethernet-broadcast-domains.html
func CreateBroadcastDomain(errorHandler *utils.ErrorHandler, r restclient.RestClient, body BroadcastDomainResourceBodyDataModelONTAP) (*BroadcastDomainGetDataModelONTAP, error) {
api := "/network/ethernet/broadcast-domains"
var bodyMap map[string]interface{}
if err := mapstructure.Decode(body, &bodyMap); err != nil {
return nil, errorHandler.MakeAndReportError(
"error encoding broadcast-domain body",
fmt.Sprintf("error on encoding %s body: %s, body: %#v", api, err, body),
)
}
query := r.NewQuery()
query.Add("return_records", "true")

statusCode, response, err := r.CallCreateMethod(api, query, bodyMap)
if err != nil {
return nil, errorHandler.MakeAndReportError(
"error creating broadcast-domain",
fmt.Sprintf("error on POST %s: %s, statusCode %d", api, err, statusCode),
)
}

var dataONTAP BroadcastDomainGetDataModelONTAP
if err := mapstructure.Decode(response.Records[0], &dataONTAP); err != nil {
return nil, errorHandler.MakeAndReportError(
"error decoding broadcast-domain info",
fmt.Sprintf("error on decode broadcast-domain info: %s, statusCode %d, response %#v", err, statusCode, response),
)
}

tflog.Debug(
errorHandler.Ctx,
fmt.Sprintf("Create broadcast_domain resource: %#v", dataONTAP),
)

return &dataONTAP, nil
}

// Update broadcast domain properties
// https://docs.netapp.com/us-en/ontap-restapi/ontap/patch-network-ethernet-broadcast-domains-.html
func UpdateBroadcastDomain(errorHandler *utils.ErrorHandler, r restclient.RestClient, body BroadcastDomainResourceBodyDataModelONTAP, id string) error {
api := "/network/ethernet/broadcast-domains/" + id
var bodyMap map[string]interface{}
if err := mapstructure.Decode(body, &bodyMap); err != nil {
return errorHandler.MakeAndReportError(
"error encoding broadcast-domain body",
fmt.Sprintf("error on encoding %s body: %s, body: %#v", api, err, body),
)
}

statusCode, _, err := r.CallUpdateMethod(api, nil, bodyMap)
if err != nil {
return errorHandler.MakeAndReportError(
"error updating broadcast-domain",
fmt.Sprintf("error on PATCH %s: %s, statusCode %d", api, err, statusCode),
)
}

tflog.Debug(
errorHandler.Ctx,
fmt.Sprintf("Update broadcast_domain resource: %#v", bodyMap),
)

return nil
}

// Delete a broadcast domain
// https://docs.netapp.com/us-en/ontap-restapi/ontap/delete-network-ethernet-broadcast-domains-.html
func DeleteBroadcastDomain(errorHandler *utils.ErrorHandler, r restclient.RestClient, id string) error {
api := "/network/ethernet/broadcast-domains/" + id

statusCode, _, err := r.CallDeleteMethod(api, nil, nil)
if err != nil {
return errorHandler.MakeAndReportError(
"error deleting broadcast-domain",
fmt.Sprintf("error on DELETE %s: %s, statusCode %d", api, err, statusCode),
)
}

tflog.Debug(
errorHandler.Ctx,
fmt.Sprintf("Delete broadcast_domain resource: %s", id),
)

return nil
}
Loading

0 comments on commit 23ad75f

Please sign in to comment.