-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/HewlettPackard/hpegl-vmaa…
…s-terraform-resources into US36658-NSX-Tsegment
- Loading branch information
Showing
8 changed files
with
286 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters