Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…s-terraform-resources into US36658-NSX-Tsegment
  • Loading branch information
Pushpalatha555 committed Oct 15, 2022
2 parents 1fa4b73 + 3285109 commit 8c53c1b
Show file tree
Hide file tree
Showing 8 changed files with 286 additions and 2 deletions.
2 changes: 1 addition & 1 deletion acc-testcases/data-sources/load_balancer_pool.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ acc:
name = "tf_POOL_DO_NOT_DELETE"
lb_id = 306
validations:
json.loadBalancerPool.id: 994
json.loadBalancerPool.id: 1019
10 changes: 10 additions & 0 deletions examples/resources/hpegl_vmaas_dhcp_server/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# (C) Copyright 2022 Hewlett Packard Enterprise Development LP

resource "hpegl_vmaas_dhcp_server" "tf_dhcp_server" {
name = "tf_dhcp_server"
lease_time = 86400
server_address = "100.96.0.1/24"
config {
edge_cluster = data.hpegl_vmaas_edge_cluster.tf_edge_cluster.provider_id
}
}
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ module github.com/HewlettPackard/hpegl-vmaas-terraform-resources
go 1.17

require (
github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.0-beta13

github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.1-0.20221011055506-84f9daed4243
github.com/golang/mock v1.6.0
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
github.com/hashicorp/terraform-plugin-docs v0.9.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.0-beta12 h1:jP6AxUfl4f7nsP
github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.0-beta12/go.mod h1:CMLvJPDveei0wZhKATZJUPyOXCJIw+nYwePYpFpvxpU=
github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.0-beta13 h1:txNHFaUxjKo5dERxk09OCBeuYNwcVdIdfyT6iHNHhLY=
github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.0-beta13/go.mod h1:CMLvJPDveei0wZhKATZJUPyOXCJIw+nYwePYpFpvxpU=
github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.1-0.20221011055506-84f9daed4243 h1:j5oA1NW+fx5UN9K7sTASgjbEQo9dOerLgL8v0nEhoRo=
github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.1-0.20221011055506-84f9daed4243/go.mod h1:gPk3WuH1P3kF3ROxyHERgKMMFHR+W0Fsspg5vsA0+O0=
github.com/Masterminds/goutils v1.1.0/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU=
github.com/Masterminds/goutils v1.1.1 h1:5nUrii3FMTL5diU80unEVvNevw1nH4+ZV4DSLVJLSYI=
github.com/Masterminds/goutils v1.1.1/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU=
Expand Down
3 changes: 3 additions & 0 deletions internal/cmp/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ func NewClient(client *apiClient.APIClient, cfg apiClient.Configuration) *Client
LoadBalancer: newLoadBalancer(
&apiClient.LoadBalancerAPIService{Client: client, Cfg: cfg},
&apiClient.RouterAPIService{Client: client, Cfg: cfg}),
DhcpServer: newDhcpServer(
&apiClient.DhcpServerAPIService{Client: client, Cfg: cfg},
&apiClient.RouterAPIService{Client: client, Cfg: cfg}),
LoadBalancerMonitor: newLoadBalancerMonitor(&apiClient.LoadBalancerAPIService{Client: client, Cfg: cfg}),
LoadBalancerProfile: newLoadBalancerProfile(&apiClient.LoadBalancerAPIService{Client: client, Cfg: cfg}),
LoadBalancerPool: newLoadBalancerPool(&apiClient.LoadBalancerAPIService{Client: client, Cfg: cfg}),
Expand Down
147 changes: 147 additions & 0 deletions internal/cmp/dhcp_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
// (C) Copyright 2022 Hewlett Packard Enterprise Development LP

package cmp

import (
"context"
"fmt"
"time"

"github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk/pkg/client"
"github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk/pkg/models"
"github.com/HewlettPackard/hpegl-vmaas-terraform-resources/internal/utils"
"github.com/tshihad/tftags"
)

type dhcpServer struct {
dhcpClient *client.DhcpServerAPIService
rClient *client.RouterAPIService
}

func newDhcpServer(dhcpServerClient *client.DhcpServerAPIService, routerClient *client.RouterAPIService) *dhcpServer {
return &dhcpServer{
dhcpClient: dhcpServerClient,
rClient: routerClient,
}
}

func (dhcp *dhcpServer) Read(ctx context.Context, d *utils.Data, meta interface{}) error {
setMeta(meta, dhcp.dhcpClient.Client)
var dhcpServerResp models.CreateNetworkDhcpServer
if err := tftags.Get(d, &dhcpServerResp); err != nil {
return err
}
getdhcpServerResp, err := dhcp.dhcpClient.GetSpecificDhcpServer(ctx, dhcpServerResp.NetworkServerID,
dhcpServerResp.ID)
if err != nil {
return err
}

return tftags.Set(d, getdhcpServerResp.GetSpecificNetworkDhcpServerResp)
}

func (dhcp *dhcpServer) Update(ctx context.Context, d *utils.Data, meta interface{}) error {
id := d.GetID()
var updateReq models.CreateNetworkDhcpServerRequest
if err := tftags.Get(d, &updateReq.NetworkDhcpServer); err != nil {
return err
}

retry := &utils.CustomRetry{
InitialDelay: time.Second * 15,
RetryDelay: time.Second * 30,
}
_, err := retry.Retry(ctx, meta, func(ctx context.Context) (interface{}, error) {
return dhcp.dhcpClient.UpdateDhcpServer(ctx,
updateReq.NetworkDhcpServer.NetworkServerID, id, updateReq)
})
if err != nil {
return err
}

return tftags.Set(d, updateReq.NetworkDhcpServer)
}

func (dhcp *dhcpServer) Create(ctx context.Context, d *utils.Data, meta interface{}) error {
setMeta(meta, dhcp.dhcpClient.Client)
var createReq models.CreateNetworkDhcpServerRequest
if err := tftags.Get(d, &createReq.NetworkDhcpServer); err != nil {
return err
}

// align createReq and fill json related fields
if err := dhcp.dhcpServerAlignRequest(ctx, meta, &createReq); err != nil {
return err
}

dhcpResp, err := dhcp.dhcpClient.CreateDhcpServer(ctx, createReq.NetworkDhcpServer.NetworkServerID,
createReq)
if err != nil {
return err
}
if !dhcpResp.Success {
return fmt.Errorf(successErr, "creating dhcp")
}
createReq.NetworkDhcpServer.ID = dhcpResp.ID

// wait until created
retry := &utils.CustomRetry{
InitialDelay: time.Second * 15,
RetryDelay: time.Second * 30,
}
_, err = retry.Retry(ctx, meta, func(ctx context.Context) (interface{}, error) {
return dhcp.dhcpClient.GetSpecificDhcpServer(ctx,
createReq.NetworkDhcpServer.NetworkServerID, createReq.NetworkDhcpServer.ID)
})
if err != nil {
return err
}

return tftags.Set(d, createReq.NetworkDhcpServer)
}

func (dhcp *dhcpServer) Delete(ctx context.Context, d *utils.Data, meta interface{}) error {
setMeta(meta, dhcp.dhcpClient.Client)
var dhcpServerResp models.CreateNetworkDhcpServer
if err := tftags.Get(d, &dhcpServerResp); err != nil {
return err
}

resp, err := dhcp.dhcpClient.DeleteDhcpServer(ctx, dhcpServerResp.NetworkServerID, dhcpServerResp.ID)
if err != nil {
return err
}

if !resp.Success {
return fmt.Errorf("got success = 'false' while deleting DHCP-SERVER")
}

return nil
}

func (dhcp *dhcpServer) dhcpServerAlignRequest(ctx context.Context, meta interface{},
createReq *models.CreateNetworkDhcpServerRequest) error {
// Get network service ID
setMeta(meta, dhcp.rClient.Client)
nsRetry := utils.CustomRetry{}
nsRetry.RetryParallel(ctx, meta, func(ctx context.Context) (interface{}, error) {
return dhcp.rClient.GetNetworkServices(ctx, nil)
})

// Align Network Server
nsResp, err := nsRetry.Wait()
if err != nil {
return err
}
networkService := nsResp.(models.GetNetworkServicesResp)

for i, n := range networkService.NetworkServices {
if n.TypeName == nsxt {
createReq.NetworkDhcpServer.ID = networkService.NetworkServices[i].ID

break
}
}

return nil
}
120 changes: 120 additions & 0 deletions internal/resources/resource_dhcp_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// (C) Copyright 2022 Hewlett Packard Enterprise Development LP

package resources

import (
"context"

"github.com/HewlettPackard/hpegl-vmaas-terraform-resources/internal/utils"
"github.com/HewlettPackard/hpegl-vmaas-terraform-resources/pkg/client"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func DhcpServer() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Description: "Provide the DHCP server name",
},
"network_server_id": {
Type: schema.TypeInt,
Description: "NSX-T Integration ID",
Computed: true,
},
"lease_time": {
Type: schema.TypeInt,
Optional: true,
Default: 86400,
Description: "Lease time for the DHCP server",
},
"server_address": {
Type: schema.TypeString,
Description: "Server address for the DHCP server",
Optional: true,
},
"config": {
Type: schema.TypeList,
Optional: true,
Description: "DHCP Server Configuration",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"edge_cluster": {
Type: schema.TypeString,
Optional: true,
Description: "Provider ID of the Edge Cluster. Use " + DSEdgeCluster + " datasource to obtain the provider_id here.",
},
},
},
},
},
SchemaVersion: 0,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
ReadContext: DhcpServerReadContext,
UpdateContext: DhcpServerUpdateContext,
CreateContext: DhcpServerCreateContext,
DeleteContext: DhcpServerDeleteContext,
Description: `Server resource facilitates creating, updating
and deleting Dhcp Server.`,
}
}

func DhcpServerReadContext(ctx context.Context, rd *schema.ResourceData, meta interface{}) diag.Diagnostics {
c, err := client.GetClientFromMetaMap(meta)
if err != nil {
return diag.FromErr(err)
}

data := utils.NewData(rd)
if err := c.CmpClient.DhcpServer.Read(ctx, data, meta); err != nil {
return diag.FromErr(err)
}

return nil
}

func DhcpServerCreateContext(ctx context.Context, rd *schema.ResourceData, meta interface{}) diag.Diagnostics {
c, err := client.GetClientFromMetaMap(meta)
if err != nil {
return diag.FromErr(err)
}

data := utils.NewData(rd)
if err := c.CmpClient.DhcpServer.Create(ctx, data, meta); err != nil {
return diag.FromErr(err)
}

return DhcpServerReadContext(ctx, rd, meta)
}

func DhcpServerUpdateContext(ctx context.Context, rd *schema.ResourceData, meta interface{}) diag.Diagnostics {
c, err := client.GetClientFromMetaMap(meta)
if err != nil {
return diag.FromErr(err)
}

data := utils.NewData(rd)
if err := c.CmpClient.DhcpServer.Update(ctx, data, meta); err != nil {
return diag.FromErr(err)
}

return nil
}

func DhcpServerDeleteContext(ctx context.Context, rd *schema.ResourceData, meta interface{}) diag.Diagnostics {
c, err := client.GetClientFromMetaMap(meta)
if err != nil {
return diag.FromErr(err)
}

data := utils.NewData(rd)
if err := c.CmpClient.DhcpServer.Delete(ctx, data, meta); err != nil {
return diag.FromErr(err)
}

return nil
}
1 change: 1 addition & 0 deletions pkg/resources/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func (r Registration) SupportedResources() map[string]*schema.Resource {
resources.ResLoadBalancerProfiles: resources.LoadBalancerProfiles(),
resources.ResLoadBalancerPools: resources.LoadBalancerPools(),
resources.ResLoadBalancerVirtualServers: resources.LoadBalancerVirtualServers(),
resources.ResDhcpServer: resources.DhcpServer(),
}
}

Expand Down

0 comments on commit 8c53c1b

Please sign in to comment.