Skip to content

Commit

Permalink
Merge pull request #245 from alrayyes/feat/null-route-history-data-so…
Browse files Browse the repository at this point in the history
…urce

feat: implement null route history data source
  • Loading branch information
alrayyes authored Dec 31, 2024
2 parents 4f576ff + 1c2df56 commit 07e17c3
Show file tree
Hide file tree
Showing 6 changed files with 575 additions and 5 deletions.
61 changes: 61 additions & 0 deletions docs/data-sources/ipmgmt_null_route_history.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "leaseweb_ipmgmt_null_route_history Data Source - leaseweb"
subcategory: ""
description: |-
Inspect null route history
---

# leaseweb_ipmgmt_null_route_history (Data Source)

Inspect null route history



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

### Optional

- `contract_id` (String) Filter by ID of the contract assigned to the IP at the time of null route creation
- `equipment_id` (String) Filter by ID of the contract assigned to the IP at the time of null route creation
- `from_date` (String) Filter by ID of the server assigned to the IP at the time of null route creation
- `from_ip` (String) Return only IPs greater or equal to the specified address
- `nulled_by` (String) Filter by the email address of the user who created the null route
- `sort` (List of String) Sort field names. Prepend the field name with '-' for descending order. E.g. `ip,-nullrouted`. Sortable field names are `ip`, `nullRouted`, `reverseLookup`
- `ticket_id` (String) Filter by the reference stored with the null route
- `to_date` (String) Return only null routes active before the specified date and time
- `to_ip` (String) Return only IPs lower or equal to the specified address
- `unnulled_by` (String) Filter by the email address of the user who removed the null route

### Read-Only

- `nullroutes` (Attributes List) (see [below for nested schema](#nestedatt--nullroutes))

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

Optional:

- `id` (String) Null route ID

Read-Only:

- `assigned_contract` (Attributes) An explanation about the purpose of this instance (see [below for nested schema](#nestedatt--nullroutes--assigned_contract))
- `automatic_unnulling_at` (String) The date and time when the null route is to be automatically removed
- `comment` (String) Comment stored with the null route
- `equipment_id` (String) ID of the equipment which was assigned to the IP at the time of null route creation
- `ip` (String) IP address
- `null_level` (Number) Null route permission level. If greater than 1 then the null route can only be removed by LeaseWeb
- `nulled_at` (String) Null route date
- `nulled_by` (String) Email address of the user who created the null route or 'LeaseWeb' if null route was created by LeaseWeb
- `ticket_id` (String) Reference stored with the null route
- `unnulled_at` (String) The date and time when the null route has been removed. If null then the null route is still active
- `unnulled_by` (String) Email address of the user who removed the null route or 'LeaseWeb' if null route was removed by LeaseWeb

<a id="nestedatt--nullroutes--assigned_contract"></a>
### Nested Schema for `nullroutes.assigned_contract`

Read-Only:

- `id` (String) ID of the contract connected to the IP
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# List null route history
data "leaseweb_ipmgmt_null_route_history" "all" {}
8 changes: 4 additions & 4 deletions internal/provider/ipmgmt/ips_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ import (
"github.com/leaseweb/terraform-provider-leaseweb/internal/utils"
)

var (
_ datasource.DataSourceWithConfigure = &ipsDataSource{}
)

type ipsDataSourceModel struct {
AssignedContractIDs []string `tfsdk:"assigned_contract_ids"`
EquipmentIDs []string `tfsdk:"equipment_ids"`
Expand Down Expand Up @@ -58,10 +62,6 @@ type subnetDataSourceModel struct {
PrefixLength types.Int32 `tfsdk:"prefix_length"`
}

var (
_ datasource.DataSourceWithConfigure = &ipsDataSource{}
)

type ipsDataSource struct{ utils.DataSourceAPI }

func (i ipsDataSource) Schema(
Expand Down
286 changes: 286 additions & 0 deletions internal/provider/ipmgmt/null_route_history_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,286 @@
package ipmgmt

import (
"context"
"strings"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/leaseweb/leaseweb-go-sdk/ipmgmt"
"github.com/leaseweb/terraform-provider-leaseweb/internal/utils"
)

var (
_ datasource.DataSourceWithConfigure = &nullRouteHistoryDataSource{}
)

type nullRouteHistoryDataSourceModel struct {
ContractID types.String `tfsdk:"contract_id"`
EquipmentID types.String `tfsdk:"equipment_id"`
FromDate types.String `tfsdk:"from_date"`
FromIP types.String `tfsdk:"from_ip"`
NulledBy types.String `tfsdk:"nulled_by"`
Sort []string `tfsdk:"sort"`
TicketID types.String `tfsdk:"ticket_id"`
ToDate types.String `tfsdk:"to_date"`
ToIP types.String `tfsdk:"to_ip"`
UnnulledBy types.String `tfsdk:"unnulled_by"`
NullRoutes []nullrouteDataSourceModel `tfsdk:"nullroutes"`
}

type nullrouteDataSourceModel struct {
AssignedContract *assignedContractDataSourceModel `tfsdk:"assigned_contract"`
AutomaticUnnullingAt types.String `tfsdk:"automatic_unnulling_at"`
Comment types.String `tfsdk:"comment"`
EquipmentID types.String `tfsdk:"equipment_id"`
ID types.String `tfsdk:"id"`
IP types.String `tfsdk:"ip"`
NulledAt types.String `tfsdk:"nulled_at"`
NulledBy types.String `tfsdk:"nulled_by"`
NullLevel types.Int32 `tfsdk:"null_level"`
TicketID types.String `tfsdk:"ticket_id"`
UnnulledAt types.String `tfsdk:"unnulled_at"`
UnnulledBy types.String `tfsdk:"unnulled_by"`
}

type nullRouteHistoryDataSource struct {
utils.DataSourceAPI
}

func (n nullRouteHistoryDataSource) Schema(
_ context.Context,
_ datasource.SchemaRequest,
response *datasource.SchemaResponse,
) {
response.Schema = schema.Schema{
Description: "Inspect null route history",
Attributes: map[string]schema.Attribute{
"contract_id": schema.StringAttribute{
Optional: true,
Description: "Filter by ID of the contract assigned to the IP at the time of null route creation",
},
"equipment_id": schema.StringAttribute{
Optional: true,
Description: "Filter by ID of the contract assigned to the IP at the time of null route creation",
},
"from_date": schema.StringAttribute{
Optional: true,
Description: "Filter by ID of the server assigned to the IP at the time of null route creation",
},
"from_ip": schema.StringAttribute{
Optional: true,
Description: "Return only IPs greater or equal to the specified address",
},
"nulled_by": schema.StringAttribute{
Optional: true,
Description: "Filter by the email address of the user who created the null route",
},
"sort": schema.ListAttribute{
ElementType: types.StringType,
Optional: true,
Description: "Sort field names. Prepend the field name with '-' for descending order. E.g. `ip,-nullrouted`. Sortable field names are `ip`, `nullRouted`, `reverseLookup`",
},
"ticket_id": schema.StringAttribute{
Optional: true,
Description: "Filter by the reference stored with the null route",
},
"to_date": schema.StringAttribute{
Optional: true,
Description: "Return only null routes active before the specified date and time",
},
"to_ip": schema.StringAttribute{
Optional: true,
Description: "Return only IPs lower or equal to the specified address",
},
"unnulled_by": schema.StringAttribute{
Optional: true,
Description: "Filter by the email address of the user who removed the null route",
},

"nullroutes": schema.ListNestedAttribute{
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"assigned_contract": schema.SingleNestedAttribute{
Description: "An explanation about the purpose of this instance",
Computed: true,
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Computed: true,
Description: "ID of the contract connected to the IP",
},
},
},
"automatic_unnulling_at": schema.StringAttribute{
Computed: true,
Description: "The date and time when the null route is to be automatically removed",
},
"comment": schema.StringAttribute{
Computed: true,
Description: "Comment stored with the null route",
},
"equipment_id": schema.StringAttribute{
Computed: true,
Description: "ID of the equipment which was assigned to the IP at the time of null route creation",
},
"id": schema.StringAttribute{
Optional: true,
Description: "Null route ID",
},
"ip": schema.StringAttribute{
Computed: true,
Description: "IP address",
},
"nulled_at": schema.StringAttribute{
Computed: true,
Description: "Null route date",
},
"nulled_by": schema.StringAttribute{
Computed: true,
Description: "Email address of the user who created the null route or 'LeaseWeb' if null route was created by LeaseWeb",
},
"null_level": schema.Int32Attribute{
Computed: true,
Description: "Null route permission level. If greater than 1 then the null route can only be removed by LeaseWeb",
},
"ticket_id": schema.StringAttribute{
Computed: true,
Description: "Reference stored with the null route",
},
"unnulled_at": schema.StringAttribute{
Computed: true,
Description: "The date and time when the null route has been removed. If null then the null route is still active",
},
"unnulled_by": schema.StringAttribute{
Computed: true,
Description: "Email address of the user who removed the null route or 'LeaseWeb' if null route was removed by LeaseWeb",
},
},
},
},
},
}
}

func (n nullRouteHistoryDataSource) Read(ctx context.Context, request datasource.ReadRequest, response *datasource.ReadResponse) {
var config nullRouteHistoryDataSourceModel
response.Diagnostics.Append(request.Config.Get(ctx, &config)...)
if response.Diagnostics.HasError() {
return
}

var nullRoutes []ipmgmt.NullRoutedIP
var offset *int32
var state nullRouteHistoryDataSourceModel

nullRouteRequest := n.IPmgmtAPI.GetNullRouteHistoryList(ctx)
if !config.ContractID.IsNull() {
nullRouteRequest = nullRouteRequest.ContractId(config.ContractID.ValueString())
state.ContractID = config.ContractID
}
if !config.EquipmentID.IsNull() {
nullRouteRequest = nullRouteRequest.EquipmentId(config.EquipmentID.ValueString())
state.EquipmentID = config.EquipmentID
}
if !config.FromDate.IsNull() {
nullRouteRequest = nullRouteRequest.FromDate(config.FromDate.ValueString())
state.FromDate = config.FromDate
}
if !config.FromIP.IsNull() {
nullRouteRequest = nullRouteRequest.FromIp(config.FromIP.ValueString())
state.FromIP = config.FromIP
}
if !config.NulledBy.IsNull() {
nullRouteRequest = nullRouteRequest.NulledBy(config.NulledBy.ValueString())
state.NulledBy = config.NulledBy
}
if len(config.Sort) > 0 {
nullRouteRequest = nullRouteRequest.Sort(strings.Join(config.Sort[:], ","))
state.Sort = config.Sort
}
if !config.TicketID.IsNull() {
nullRouteRequest = nullRouteRequest.TicketId(config.TicketID.ValueString())
state.TicketID = config.TicketID
}
if !config.ToDate.IsNull() {
nullRouteRequest = nullRouteRequest.ToDate(config.ToDate.ValueString())
state.ToDate = config.ToDate
}
if !config.ToIP.IsNull() {
nullRouteRequest = nullRouteRequest.ToIp(config.ToIP.ValueString())
state.ToIP = config.ToIP
}
if !config.UnnulledBy.IsNull() {
nullRouteRequest = nullRouteRequest.UnnulledBy(config.UnnulledBy.ValueString())
state.UnnulledBy = config.UnnulledBy
}

for {
result, httpResponse, err := nullRouteRequest.Execute()
if err != nil {
response.Diagnostics.AddError(err.Error(), err.Error())
utils.SdkError(ctx, &response.Diagnostics, err, httpResponse)
return
}

nullRoutes = append(nullRoutes, result.GetNullroutes()...)

metadata := result.GetMetadata()

offset = utils.NewOffset(
metadata.GetLimit(),
metadata.GetOffset(),
metadata.GetTotalCount(),
)

if offset == nil {
break
}

nullRouteRequest = nullRouteRequest.Offset(*offset)
}

for _, sdkNullRoute := range nullRoutes {
var assignedContract *assignedContractDataSourceModel
sdkAssignedContract, _ := sdkNullRoute.GetAssignedContractOk()
if sdkAssignedContract != nil {
assignedContract = &assignedContractDataSourceModel{
ID: basetypes.NewStringValue(sdkAssignedContract.GetId()),
}
}

automaticUnnullingAt, _ := sdkNullRoute.GetAutomatedUnnullingAtOk()
comment, _ := sdkNullRoute.GetCommentOk()
ticketID, _ := sdkNullRoute.GetTicketIdOk()
unnulledAt, _ := sdkNullRoute.GetUnnulledAtOk()
unnulledBy, _ := sdkNullRoute.GetUnnulledByOk()

nullRoute := nullrouteDataSourceModel{
AssignedContract: assignedContract,
AutomaticUnnullingAt: utils.AdaptNullableTimeToStringValue(automaticUnnullingAt),
Comment: basetypes.NewStringPointerValue(comment),
EquipmentID: basetypes.NewStringValue(sdkNullRoute.GetEquipmentId()),
ID: basetypes.NewStringValue(sdkNullRoute.GetId()),
IP: basetypes.NewStringValue(sdkNullRoute.GetIp()),
NulledAt: basetypes.NewStringValue(sdkNullRoute.GetNulledAt().String()),
NulledBy: basetypes.NewStringValue(sdkNullRoute.GetNulledBy()),
NullLevel: basetypes.NewInt32Value(sdkNullRoute.GetNullLevel()),
TicketID: basetypes.NewStringPointerValue(ticketID),
UnnulledAt: utils.AdaptNullableTimeToStringValue(unnulledAt),
UnnulledBy: basetypes.NewStringPointerValue(unnulledBy),
}
state.NullRoutes = append(state.NullRoutes, nullRoute)
}

response.Diagnostics.Append(response.State.Set(ctx, state)...)
}

func NewNullRouteHistoryDataSource() datasource.DataSource {
return &nullRouteHistoryDataSource{
DataSourceAPI: utils.DataSourceAPI{
Name: "ipmgmt_null_route_history",
},
}
}
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ func (p *leasewebProvider) DataSources(_ context.Context) []func() datasource.Da
publiccloud.NewISOsDataSource,
dns.NewResourceRecordSetsDataSource,
ipmgmt.NewIPsDataSource,
ipmgmt.NewNullRouteHistoryDataSource,
}
}

Expand Down
Loading

0 comments on commit 07e17c3

Please sign in to comment.