Skip to content

Commit

Permalink
[DDIA-308] Assign Service Instance in DHCP Config Profile (#121)
Browse files Browse the repository at this point in the history
* Added DHCP Hosts resource config

* Added support for retry and timeouts

* Added a new schema

* Resolved lint errors

* Fixed tag issue

* Implemented the review changes

* Formatting changes

* Updated Example

* Fixed issues

* Reverted changes in data source schema

* Generated docs
  • Loading branch information
unasra authored Jun 17, 2024
1 parent 382697d commit ff2f428
Show file tree
Hide file tree
Showing 11 changed files with 1,385 additions and 821 deletions.
80 changes: 80 additions & 0 deletions docs/resources/dhcp_host.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "bloxone_dhcp_host Resource - terraform-provider-bloxone"
subcategory: "IPAM/DHCP"
description: |-
Manages DHCP Hosts.
A DHCP Host object associates a DHCP Config Profile with an on-prem host.
Note: This resource represents an existing backend object that cannot be created or deleted through API calls. Instead, it can only be updated. When using terraform apply the resource configuration is applied to the existing object, and no new object is created. Similarly terraform destroy removes the configuration associated with the object without actually deleting it from the backend.
---

# bloxone_dhcp_host (Resource)

Manages DHCP Hosts.

A DHCP Host object associates a DHCP Config Profile with an on-prem host.

Note: This resource represents an existing backend object that cannot be created or deleted through API calls. Instead, it can only be updated. When using terraform apply the resource configuration is applied to the existing object, and no new object is created. Similarly terraform destroy removes the configuration associated with the object without actually deleting it from the backend.

## Example Usage

```terraform
data "bloxone_infra_hosts" "host" {
filters = {
display_name = "my-host"
}
}
resource "bloxone_dhcp_server" "dhcp_server" {
name = "my-dhcp-server"
}
resource "bloxone_dhcp_host" "example_dhcp_host" {
id = data.bloxone_infra_hosts.host.results.0.legacy_id
server = bloxone_dhcp_server.dhcp_server.id
}
```

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

### Required

- `id` (String) The resource identifier.
- `server` (String) The resource identifier.

### Optional

- `retry_if_not_found` (Boolean) If set to `true`, the resource will retry until a matching host is found, or until the Create Timeout expires.
- `timeouts` (Block, Optional) (see [below for nested schema](#nestedblock--timeouts))

### Read-Only

- `address` (String) The primary IP address of the on-prem host.
- `anycast_addresses` (List of String) Anycast address configured to the host. Order is not significant.
- `associated_server` (Attributes) The DHCP Config Profile for the on-prem host. (see [below for nested schema](#nestedatt--associated_server))
- `comment` (String) The description for the on-prem host.
- `current_version` (String) Current dhcp application version of the host.
- `ip_space` (String) The resource identifier.
- `name` (String) The display name of the on-prem host.
- `ophid` (String) The on-prem host ID.
- `provider_id` (String) External provider identifier.
- `tags` (Map of String) The tags of the on-prem host in JSON format.
- `tags_all` (Map of String) The tags of the on-prem host in JSON format including default tags.
- `type` (String) Defines the type of host. Allowed values: * _bloxone_ddi_: host type is BloxOne DDI, * _microsoft_azure_: host type is Microsoft Azure, * _amazon_web_service_: host type is Amazon Web Services. * _microsoft_active_directory_: host type is Microsoft Active Directory.

<a id="nestedblock--timeouts"></a>
### Nested Schema for `timeouts`

Optional:

- `create` (String) A string that can be [parsed as a duration](https://pkg.go.dev/time#ParseDuration) consisting of numbers and unit suffixes, such as "30s" or "2h45m". Valid time units are "s" (seconds), "m" (minutes), "h" (hours).


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

Read-Only:

- `id` (String) The resource identifier.
- `name` (String) The DHCP Config Profile name.
14 changes: 14 additions & 0 deletions examples/resources/bloxone_dhcp_host/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
data "bloxone_infra_hosts" "host" {
filters = {
display_name = "my-host"
}
}

resource "bloxone_dhcp_server" "dhcp_server" {
name = "my-dhcp-server"
}

resource "bloxone_dhcp_host" "example_dhcp_host" {
id = data.bloxone_infra_hosts.host.results.0.legacy_id
server = bloxone_dhcp_server.dhcp_server.id
}
96 changes: 48 additions & 48 deletions internal/acctest/acctest.go
Original file line number Diff line number Diff line change
@@ -1,86 +1,86 @@
package acctest

import (
"fmt"
"math/rand"
"os"
"testing"
"fmt"
"math/rand"
"os"
"testing"

"github.com/hashicorp/terraform-plugin-framework/providerserver"
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-framework/providerserver"
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"

bloxoneclient "github.com/infobloxopen/bloxone-go-client/client"
"github.com/infobloxopen/bloxone-go-client/option"
"github.com/infobloxopen/terraform-provider-bloxone/internal/provider"
bloxoneclient "github.com/infobloxopen/bloxone-go-client/client"
"github.com/infobloxopen/bloxone-go-client/option"
"github.com/infobloxopen/terraform-provider-bloxone/internal/provider"
)

const (
letterBytes = "abcdefghijklmnopqrstuvwxyz"
defaultKey = "managed_by"
defaultValue = "terraform"
letterBytes = "abcdefghijklmnopqrstuvwxyz"
defaultKey = "managed_by"
defaultValue = "terraform"
)

var (
// BloxOneClient will be used to do verification tests
BloxOneClient *bloxoneclient.APIClient
// BloxOneClient will be used to do verification tests
BloxOneClient *bloxoneclient.APIClient

// ProtoV6ProviderFactories are used to instantiate a provider during
// acceptance testing. The factory function will be invoked for every Terraform
// CLI command executed to create a provider server to which the CLI can
// reattach.
ProtoV6ProviderFactories = map[string]func() (tfprotov6.ProviderServer, error){
"bloxone": providerserver.NewProtocol6WithError(provider.New("test", "test")()),
}
ProtoV6ProviderFactoriesWithTags = map[string]func() (tfprotov6.ProviderServer, error){
"bloxone": providerserver.NewProtocol6WithError(provider.NewWithTags(map[string]string{defaultKey: defaultValue})()),
}
// ProtoV6ProviderFactories are used to instantiate a provider during
// acceptance testing. The factory function will be invoked for every Terraform
// CLI command executed to create a provider server to which the CLI can
// reattach.
ProtoV6ProviderFactories = map[string]func() (tfprotov6.ProviderServer, error){
"bloxone": providerserver.NewProtocol6WithError(provider.New("test", "test")()),
}
ProtoV6ProviderFactoriesWithTags = map[string]func() (tfprotov6.ProviderServer, error){
"bloxone": providerserver.NewProtocol6WithError(provider.NewWithTags(map[string]string{defaultKey: defaultValue})()),
}
)

// RandomNameWithPrefix generates a random name with the given prefix.
// This is used in the acceptance tests where a unique name is required for the resource.
func RandomNameWithPrefix(prefix string) string {
return fmt.Sprintf("%s-%s", prefix, RandomName())
return fmt.Sprintf("%s-%s", prefix, RandomName())
}

func RandomIP() string {
return fmt.Sprintf("%d.%d.%d.%d", rand.Intn(255), rand.Intn(255), rand.Intn(255), rand.Intn(255))
return fmt.Sprintf("%d.%d.%d.%d", rand.Intn(255), rand.Intn(255), rand.Intn(255), rand.Intn(255))
}

func RandomName() string {
b := make([]byte, 6)
for i := range b {
b[i] = letterBytes[rand.Intn(len(letterBytes))]
}
return string(b)
b := make([]byte, 6)
for i := range b {
b[i] = letterBytes[rand.Intn(len(letterBytes))]
}
return string(b)
}

func PreCheck(t *testing.T) {
cspURL := os.Getenv("BLOXONE_CSP_URL")
if cspURL == "" {
t.Fatal("BLOXONE_CSP_URL must be set for acceptance tests")
}
cspURL := os.Getenv("BLOXONE_CSP_URL")
if cspURL == "" {
t.Fatal("BLOXONE_CSP_URL must be set for acceptance tests")
}

apiKey := os.Getenv("BLOXONE_API_KEY")
if apiKey == "" {
t.Fatal("BLOXONE_API_KEY must be set for acceptance tests")
}
apiKey := os.Getenv("BLOXONE_API_KEY")
if apiKey == "" {
t.Fatal("BLOXONE_API_KEY must be set for acceptance tests")
}

BloxOneClient = bloxoneclient.NewAPIClient(
option.WithClientName("terraform-acceptance-tests"),
option.WithCSPUrl(cspURL),
option.WithAPIKey(apiKey),
option.WithDebug(true),
)
BloxOneClient = bloxoneclient.NewAPIClient(
option.WithClientName("terraform-acceptance-tests"),
option.WithCSPUrl(cspURL),
option.WithAPIKey(apiKey),
option.WithDebug(true),
)
}

func VerifyDefaultTag(resourceName string) resource.TestCheckFunc {
return resource.TestCheckResourceAttr(resourceName, fmt.Sprintf("tags_all.%s", defaultKey), defaultValue)
return resource.TestCheckResourceAttr(resourceName, fmt.Sprintf("tags_all.%s", defaultKey), defaultValue)
}

// TestAccBase_DhcpHosts creates a Terraform datasource config that allows you to filter by tags
func TestAccBase_DhcpHosts() string {
return `
return `
data "bloxone_dhcp_hosts" "test" {
tag_filters = {
used_for = "Terraform Provider Acceptance Tests"
Expand Down
Loading

0 comments on commit ff2f428

Please sign in to comment.