Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
39 changes: 39 additions & 0 deletions docs/list-resources/ipam_bulk_hostname_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "nios_ipam_bulk_hostname_template List Resource - nios"
subcategory: "IPAM"
description: |-
Query existing ipam Bulkhostnametemplate.
---

# nios_ipam_bulk_hostname_template (List Resource)

Query existing ipam Bulkhostnametemplate.

## Example Usage

```terraform
// List specific Bulk Hostname Templates using filters
list "nios_ipam_bulk_hostname_template" "list_bulk_hostname_templates_using_filters" {
provider = nios
config {
filters = {
template_name = "example_template"
}
}
}

// List Bulk Hostname Templates with resource details included
list "nios_ipam_bulk_hostname_template" "list_bulk_hostname_templates_with_resource" {
provider = nios
include_resource = true
}
```

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

### Optional

- `extattrfilters` (Map of String) External Attribute Filters are used to return a more specific list of results by filtering on external attributes. If you specify multiple filters, the results returned will have only resources that match all the specified filters.
- `filters` (Map of String) Filters are used to return a more specific list of results. Filters can be used to match resources by specific attributes, e.g. name. If you specify multiple filters, the results returned will have only resources that match all the specified filters.
49 changes: 49 additions & 0 deletions docs/list-resources/ipam_vlanview.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "nios_ipam_vlanview List Resource - nios"
subcategory: "IPAM"
description: |-
Query existing ipam Vlanview.
---

# nios_ipam_vlanview (List Resource)

Query existing ipam Vlanview.

## Example Usage

```terraform
// List specific VLAN Views using filters
list "nios_ipam_vlanview" "list_vlanviews_using_filters" {
provider = nios
config {
filters = {
name = "example_vlan_view"
}
}
}

// List specific VLAN Views using Extensible Attributes
list "nios_ipam_vlanview" "list_vlanviews_using_extensible_attributes" {
provider = nios
config {
extattrfilters = {
Site = "location-1"
}
}
}

// List VLAN Views with resource details included
list "nios_ipam_vlanview" "list_vlanviews_with_resource" {
provider = nios
include_resource = true
}
```

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

### Optional

- `extattrfilters` (Map of String) External Attribute Filters are used to return a more specific list of results by filtering on external attributes. If you specify multiple filters, the results returned will have only resources that match all the specified filters.
- `filters` (Map of String) Filters are used to return a more specific list of results. Filters can be used to match resources by specific attributes, e.g. name. If you specify multiple filters, the results returned will have only resources that match all the specified filters.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// List specific Bulk Hostname Templates using filters
list "nios_ipam_bulk_hostname_template" "list_bulk_hostname_templates_using_filters" {
provider = nios
config {
filters = {
template_name = "example_template"
}
}
}

// List Bulk Hostname Templates with resource details included
list "nios_ipam_bulk_hostname_template" "list_bulk_hostname_templates_with_resource" {
provider = nios
include_resource = true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// List specific VLAN Views using filters
list "nios_ipam_vlanview" "list_vlanviews_using_filters" {
provider = nios
config {
filters = {
name = "example_vlan_view"
}
}
}

// List specific VLAN Views using Extensible Attributes
list "nios_ipam_vlanview" "list_vlanviews_using_extensible_attributes" {
provider = nios
config {
extattrfilters = {
Site = "location-1"
}
}
}

// List VLAN Views with resource details included
list "nios_ipam_vlanview" "list_vlanviews_with_resource" {
provider = nios
include_resource = true
}

2 changes: 2 additions & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,8 @@ func (p *NIOSProvider) ListResources(ctx context.Context) []func() list.ListReso
dhcp.NewFixedaddressList,

ipam.NewNetworkviewList,
ipam.NewBulkhostnametemplateList,
ipam.NewVlanviewList,
}
}

Expand Down
197 changes: 197 additions & 0 deletions internal/service/ipam/bulkhostnametemplate_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
package ipam

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/list"
"github.com/hashicorp/terraform-plugin-framework/list/schema"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"

niosclient "github.com/infobloxopen/infoblox-nios-go-client/client"
"github.com/infobloxopen/infoblox-nios-go-client/ipam"

"github.com/infobloxopen/terraform-provider-nios/internal/flex"
"github.com/infobloxopen/terraform-provider-nios/internal/utils"
)

// Ensure provider defined types fully satisfy framework interfaces.
var _ list.ListResource = &BulkhostnametemplateList{}
var _ list.ListResourceWithConfigure = &BulkhostnametemplateList{}

func NewBulkhostnametemplateList() list.ListResource {
return &BulkhostnametemplateList{}
}

// BulkhostnametemplateList defines the List implementation.
type BulkhostnametemplateList struct {
client *niosclient.APIClient
}

func (l *BulkhostnametemplateList) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_" + "ipam_bulk_hostname_template"
}

func (l *BulkhostnametemplateList) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
// Prevent panic if the provider has not been configured.
if req.ProviderData == nil {
return
}

client, ok := req.ProviderData.(*niosclient.APIClient)

if !ok {
resp.Diagnostics.AddError(
"Unexpected List Resource Configure Type",
fmt.Sprintf("Expected *niosclient.APIClient, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}

l.client = client
}

type BulkhostnametemplateListModel struct {
Filters types.Map `tfsdk:"filters"`
ExtAttrFilters types.Map `tfsdk:"extattrfilters"`
}

func (l *BulkhostnametemplateList) ListResourceConfigSchema(ctx context.Context, req list.ListResourceSchemaRequest, resp *list.ListResourceSchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "Query existing ipam Bulkhostnametemplate.",
Attributes: map[string]schema.Attribute{
"filters": schema.MapAttribute{
MarkdownDescription: "Filters are used to return a more specific list of results. Filters can be used to match resources by specific attributes, e.g. name. If you specify multiple filters, the results returned will have only resources that match all the specified filters.",
ElementType: types.StringType,
Optional: true,
},
"extattrfilters": schema.MapAttribute{
MarkdownDescription: "External Attribute Filters are used to return a more specific list of results by filtering on external attributes. If you specify multiple filters, the results returned will have only resources that match all the specified filters.",
ElementType: types.StringType,
Optional: true,
},
},
}
}

func (l *BulkhostnametemplateList) List(ctx context.Context, req list.ListRequest, stream *list.ListResultsStream) {
var data BulkhostnametemplateListModel
pageCount := 0
limit := int32(req.Limit)

diags := req.Config.Get(ctx, &data)
if diags.HasError() {
stream.Results = list.ListResultsStreamDiagnostics(diags)
return
}

allResults, err := utils.ReadWithPages(
func(pageID string, maxResultsPerPage int32) ([]ipam.Bulkhostnametemplate, string, error) {

var paging int32 = 1

// If total limit is set by user and is less than maxResultsPerPage, use it as maxResultsPerPage for API call to optimize the number of results.
// If limit > maxResultsPerPage, terraform automatically breaks connection to the provider after limit is reached.
if limit < maxResultsPerPage {
maxResultsPerPage = limit
}

//Increment the page count
pageCount++

request := l.client.IPAMAPI.
BulkhostnametemplateAPI.
List(ctx).
Filters(flex.ExpandFrameworkMapString(ctx, data.Filters, &diags)).
ReturnAsObject(1).
ReturnFieldsPlus(readableAttributesForBulkhostnametemplate).
Paging(paging).
MaxResults(maxResultsPerPage)
Comment on lines +105 to +112

// Add page ID if provided
if pageID != "" {
request = request.PageId(pageID)
}

// Execute the request
apiRes, _, err := request.Execute()
if err != nil {
return nil, "", err
}

res := apiRes.ListBulkhostnametemplateResponseObject.GetResult()
tflog.Info(ctx, fmt.Sprintf("Page %d : Retrieved %d results", pageCount, len(res)))

// Check for next page ID in additional properties
additionalProperties := apiRes.ListBulkhostnametemplateResponseObject.AdditionalProperties
var nextPageID string

// If limit is reached , we do not need to continue to make API calls, we can return the results and empty nextPageID to stop pagination.
if len(res) >= int(limit) {
nextPageID = ""
tflog.Info(ctx, "Limit reached, stopped fetching more pages.")
return res, nextPageID, nil
}

npId, ok := additionalProperties["next_page_id"]
if ok {
if npIdStr, ok := npId.(string); ok {
nextPageID = npIdStr
}
} else {
tflog.Info(ctx, "No next page ID found. This is the last page.")
}
return res, nextPageID, nil
},
)

if err != nil {
diags.AddError("Client Error", fmt.Sprintf("Unable to list Bulkhostnametemplate, got error: %s", err))
stream.Results = list.ListResultsStreamDiagnostics(diags)
return
}

stream.Results = func(push func(list.ListResult) bool) {
for _, item := range allResults {
result := req.NewListResult(ctx)

// Set the Identity for each result
result.Diagnostics.Append(result.Identity.SetAttribute(ctx, path.Root("ref"), &item.Ref)...)
if result.Diagnostics.HasError() {
if !push(result) {
return
}
continue
}

// By default, list only returns the identity.
// If IncludeResource is true, it gets the full resource and sets it in the result.Resource
if req.IncludeResource {
if result.Diagnostics.HasError() {
if !push(result) {
return
}
continue
}
result1 := FlattenBulkhostnametemplate(ctx, &item, &result.Diagnostics)
result.Diagnostics.Append(result.Resource.Set(ctx, &result1)...)
if result.Diagnostics.HasError() {
if !push(result) {
return
}
continue
}

}

// Push the result to the stream
if !push(result) {
return
}
}
}

}
Loading
Loading