-
Notifications
You must be signed in to change notification settings - Fork 35
feat: add cidr_list attribute to stackit_public_ip_ranges datasource #1001
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
h3adex
wants to merge
1
commit into
stackitcloud:main
Choose a base branch
from
h3adex:feat/add-reabable-ip-list
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
18 changes: 17 additions & 1 deletion
18
examples/data-sources/stackit_public_ip_ranges/data-source.tf
This file contains hidden or 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 |
---|---|---|
@@ -1 +1,17 @@ | ||
data "stackit_public_ip_ranges" "example" {} | ||
data "stackit_public_ip_ranges" "example" {} | ||
|
||
locals { | ||
vpn_cidrs = ["X.X.X.X/32", "X.X.X.X/24"] | ||
} | ||
|
||
# example usage: allow stackit services and customer vpn cidr to access observability apis | ||
resource "stackit_observability_instance" "example" { | ||
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" | ||
name = "example-instance" | ||
plan_name = "Observability-Monitoring-Medium-EU01" | ||
# Allow all stackit services and customer vpn cidr to access observability apis | ||
acl = concat(data.stackit_public_ip_ranges.example.cidr_list, local.vpn_cidrs) | ||
metrics_retention_days = 90 | ||
metrics_retention_days_5m_downsampling = 90 | ||
metrics_retention_days_1h_downsampling = 90 | ||
} |
This file contains hidden or 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 hidden or 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 |
---|---|---|
|
@@ -41,6 +41,7 @@ type publicIpRangesDataSource struct { | |
type Model struct { | ||
Id types.String `tfsdk:"id"` // needed by TF | ||
PublicIpRanges types.List `tfsdk:"public_ip_ranges"` | ||
CidrList types.List `tfsdk:"cidr_list"` | ||
} | ||
|
||
var publicIpRangesTypes = map[string]attr.Type{ | ||
|
@@ -97,6 +98,11 @@ func (d *publicIpRangesDataSource) Schema(_ context.Context, _ datasource.Schema | |
}, | ||
}, | ||
}, | ||
"cidr_list": schema.ListAttribute{ | ||
Description: "A list of IP range strings (CIDRs) extracted from the public_ip_ranges for easy consumption.", | ||
ElementType: types.StringType, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
@@ -155,18 +161,19 @@ func mapFields(ctx context.Context, publicIpRangeResp *iaas.PublicNetworkListRes | |
} | ||
|
||
// mapPublicIpRanges map the response publicIpRanges to the model | ||
func mapPublicIpRanges(_ context.Context, publicIpRanges *[]iaas.PublicNetwork, model *Model) error { | ||
func mapPublicIpRanges(ctx context.Context, publicIpRanges *[]iaas.PublicNetwork, model *Model) error { | ||
if publicIpRanges == nil { | ||
return fmt.Errorf("publicIpRanges input is nil") | ||
} | ||
if len(*publicIpRanges) == 0 { | ||
model.PublicIpRanges = types.ListNull(types.ObjectType{AttrTypes: publicIpRangesTypes}) | ||
model.CidrList = types.ListNull(types.StringType) | ||
return nil | ||
} | ||
|
||
var apiIpRanges []string | ||
for _, ipRange := range *publicIpRanges { | ||
if ipRange.Cidr != nil || *ipRange.Cidr != "" { | ||
if ipRange.Cidr != nil && *ipRange.Cidr != "" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch :) |
||
apiIpRanges = append(apiIpRanges, *ipRange.Cidr) | ||
} | ||
} | ||
|
@@ -197,5 +204,12 @@ func mapPublicIpRanges(_ context.Context, publicIpRanges *[]iaas.PublicNetwork, | |
} | ||
|
||
model.PublicIpRanges = ipRangesTF | ||
|
||
cidrListTF, diags := types.ListValueFrom(ctx, types.StringType, apiIpRanges) | ||
if diags.HasError() { | ||
return core.DiagsToError(diags) | ||
} | ||
model.CidrList = cidrListTF | ||
|
||
return nil | ||
} |
115 changes: 115 additions & 0 deletions
115
stackit/internal/services/iaas/publicipranges/datasource_test.go
This file contains hidden or 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,115 @@ | ||
package publicipranges | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/hashicorp/terraform-plugin-framework/attr" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
coreUtils "github.com/stackitcloud/stackit-sdk-go/core/utils" | ||
"github.com/stackitcloud/stackit-sdk-go/services/iaas" | ||
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils" | ||
) | ||
|
||
func TestMapPublicIpRanges(t *testing.T) { | ||
ctx := context.Background() | ||
|
||
tests := []struct { | ||
name string | ||
input *[]iaas.PublicNetwork | ||
expected Model | ||
isValid bool | ||
}{ | ||
{ | ||
name: "nil input should return error", | ||
input: nil, | ||
isValid: false, | ||
}, | ||
{ | ||
name: "empty input should return nulls", | ||
input: &[]iaas.PublicNetwork{}, | ||
expected: Model{ | ||
PublicIpRanges: types.ListNull(types.ObjectType{AttrTypes: publicIpRangesTypes}), | ||
CidrList: types.ListNull(types.StringType), | ||
}, | ||
isValid: true, | ||
}, | ||
{ | ||
name: "valid cidr entries", | ||
input: &[]iaas.PublicNetwork{ | ||
{Cidr: coreUtils.Ptr("192.168.0.0/24")}, | ||
{Cidr: coreUtils.Ptr("192.168.1.0/24")}, | ||
}, | ||
expected: func() Model { | ||
cidrs := []string{"192.168.0.0/24", "192.168.1.0/24"} | ||
ipRangesList := make([]attr.Value, 0, len(cidrs)) | ||
for _, cidr := range cidrs { | ||
ipRange, _ := types.ObjectValue(publicIpRangesTypes, map[string]attr.Value{ | ||
"cidr": types.StringValue(cidr), | ||
}) | ||
ipRangesList = append(ipRangesList, ipRange) | ||
} | ||
ipRangesVal, _ := types.ListValue(types.ObjectType{AttrTypes: publicIpRangesTypes}, ipRangesList) | ||
cidrListVal, _ := types.ListValueFrom(ctx, types.StringType, cidrs) | ||
|
||
return Model{ | ||
PublicIpRanges: ipRangesVal, | ||
CidrList: cidrListVal, | ||
Id: utils.BuildInternalTerraformId(cidrs...), | ||
} | ||
}(), | ||
isValid: true, | ||
}, | ||
{ | ||
name: "filter out empty CIDRs", | ||
input: &[]iaas.PublicNetwork{ | ||
{Cidr: coreUtils.Ptr("")}, | ||
{Cidr: nil}, | ||
{Cidr: coreUtils.Ptr("10.0.0.0/8")}, | ||
}, | ||
expected: func() Model { | ||
cidrs := []string{"10.0.0.0/8"} | ||
ipRange, _ := types.ObjectValue(publicIpRangesTypes, map[string]attr.Value{ | ||
"cidr": types.StringValue("10.0.0.0/8"), | ||
}) | ||
ipRangesVal, _ := types.ListValue(types.ObjectType{AttrTypes: publicIpRangesTypes}, []attr.Value{ipRange}) | ||
cidrListVal, _ := types.ListValueFrom(ctx, types.StringType, cidrs) | ||
return Model{ | ||
PublicIpRanges: ipRangesVal, | ||
CidrList: cidrListVal, | ||
Id: utils.BuildInternalTerraformId(cidrs...), | ||
} | ||
}(), | ||
isValid: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
var model Model | ||
err := mapPublicIpRanges(ctx, tt.input, &model) | ||
|
||
if !tt.isValid { | ||
if err == nil { | ||
t.Fatalf("Expected error but got nil") | ||
} | ||
return | ||
} else if err != nil { | ||
t.Fatalf("Unexpected error: %v", err) | ||
} | ||
|
||
if diff := cmp.Diff(tt.expected.Id, model.Id); diff != "" { | ||
t.Errorf("ID does not match:\n%s", diff) | ||
} | ||
|
||
if diff := cmp.Diff(tt.expected.CidrList, model.CidrList); diff != "" { | ||
t.Errorf("cidr_list does not match:\n%s", diff) | ||
} | ||
|
||
if diff := cmp.Diff(tt.expected.PublicIpRanges, model.PublicIpRanges); diff != "" { | ||
t.Errorf("public_ip_ranges does not match:\n%s", diff) | ||
} | ||
}) | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
stackit/internal/services/iaas/testdata/datasource-public-ip-ranges.tf
This file contains hidden or 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 @@ | ||
data "stackit_public_ip_ranges" "example" {} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR looks in general good. Only a nitpick, can you adjust the comment like in the suggestion, to separate more between the simple example of the datasource and how it can be used with with locals