Skip to content

Commit

Permalink
add minus characters to base cidr filename
Browse files Browse the repository at this point in the history
  • Loading branch information
sbehl-it4ipm committed May 8, 2023
1 parent 50bd3db commit a7c7d87
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 18 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ website/node_modules
*.iml
*.test
*.iml
.terraform**

website/vendor

Expand Down
6 changes: 6 additions & 0 deletions examples/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ resource "cidr-reservator_network_request" "network_request" {
netmask_id = "test"
}

resource "cidr-reservator_network_request" "network_request3" {
prefix_length = 26
base_cidr = "10.5.0.0/16"
netmask_id = "test2"
}

resource "cidr-reservator_network_request" "network_request2" {
prefix_length = 26
base_cidr = "10.6.0.0/18"
Expand Down
3 changes: 1 addition & 2 deletions internal/provider/cidrCalculator/cidrCalculator.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ func (c cidrCalculator) GetNextNetmask() (string, error) {
if err != nil {
return "", err
}
firstCidrSubnetFirstIP, firstCidrSubnetLastIP := cidr.AddressRange(firstCidrSubnet)
if len(ipNets) == 0 || (!ipNets[len(ipNets)-1].Contains(firstCidrSubnetFirstIP) && !ipNets[len(ipNets)-1].Contains(firstCidrSubnetLastIP)) {
if len(ipNets) == 0 || (cidr.VerifyNoOverlap(append(ipNets, firstCidrSubnet), c.baseIPNet) == nil) {
nextIPNet = firstCidrSubnet
} else {
nextIPNet, err = c.recursivelyFindNextNetmask(&ipNets, c.prefixLength, false)
Expand Down
6 changes: 3 additions & 3 deletions internal/provider/cidrCalculator/cidrCalculator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ type TestData struct {

func initTestData() *TestData {
return &TestData{
currentSubnets: &map[string]string{"test1": "10.116.0.16/29", "test3": "10.116.0.0/29", "test4": "10.116.3.0/24", "test5": "10.116.8.0/22", "test6": "10.116.4.0/22", "foo": "10.116.2.0/24", "tralala": "10.116.1.0/24", "tut": "10.116.0.8/29", "fioo": "10.116.0.32/28", "tutut": "10.119.0.0/16"},
prefixLength: 25,
currentSubnets: &map[string]string{"test1": "10.116.0.16/29", "test4": "10.116.3.0/24", "test5": "10.116.8.0/22", "test6": "10.116.4.0/22", "foo": "10.116.2.0/24", "tralala": "10.116.1.0/24", "tut": "10.116.0.8/29", "fioo": "10.116.0.32/28", "tutut": "10.119.0.0/16"},
prefixLength: 22,
baseCidrRange: "10.116.0.0/14",
}
}
Expand All @@ -29,7 +29,7 @@ func TestCorrectNextCidr(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if netmask != "10.116.0.128/25" {
if netmask != "10.116.12.0/22" {
t.Fatalf("Unexpected value for next netmask %s", netmask)
}
}
Expand Down
20 changes: 8 additions & 12 deletions internal/provider/connector/gcpConnector.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,17 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/hashicorp/terraform-plugin-log/tflog"
"io"
"strings"
"time"
)

type GcpConnector struct {
BucketName string
BaseCidrRange string
FileName string
FileNameWithoutMinus string
generation int64
FileWithoutMinusExists bool
BucketName string
BaseCidrRange string
FileName string
generation int64
}

type NetworkConfig struct {
Expand All @@ -26,8 +25,7 @@ type NetworkConfig struct {

func New(bucketName string, baseCidr string) GcpConnector {
fileName := fmt.Sprintf("cidr-reservation/baseCidr-%s.json", strings.Replace(strings.Replace(baseCidr, ".", "-", -1), "/", "-", -1))
fileNameWithoutMinus := fmt.Sprintf("cidr-reservation/baseCidr-%s.json", strings.Replace(strings.Replace(baseCidr, ".", "", -1), "/", "", -1))
return GcpConnector{bucketName, baseCidr, fileName, fileNameWithoutMinus, -1}
return GcpConnector{bucketName, baseCidr, fileName, -1}
}

func (gcp *GcpConnector) ReadRemote(ctx context.Context) (*NetworkConfig, error) {
Expand All @@ -44,16 +42,13 @@ func (gcp *GcpConnector) ReadRemote(ctx context.Context) (*NetworkConfig, error)
if err != nil {
return nil, err
}
objectHandle := bucket.Object(gcp.FileNameWithoutMinus)
objectHandle := bucket.Object(gcp.FileName)
attrs, err := objectHandle.Attrs(ctx)
if err == nil {
gcp.generation = attrs.Generation
}
rc, err := objectHandle.NewReader(ctx)
if err != nil {
if err == storage.ErrObjectNotExist {
objectHandle = bucket.Object(gcp.FileName)
}
return &networkConfig, err
}
defer rc.Close()
Expand Down Expand Up @@ -88,6 +83,7 @@ func (gcp *GcpConnector) WriteRemote(networkConfig *NetworkConfig, ctx context.C
}
_, _ = writer.Write(marshalled)
if err := writer.Close(); err != nil {
tflog.Error(ctx, "Failed to write file to GCP", map[string]interface{}{"error": err, "generation": gcp.generation})
return err
}
return nil
Expand Down
1 change: 0 additions & 1 deletion internal/provider/resource_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ func importState(ctx context.Context, data *schema.ResourceData, i interface{})
}

func readRemote(ctx context.Context, data *schema.ResourceData, m interface{}) (*connector.NetworkConfig, *connector.GcpConnector, error) {

cidrProviderBucket := m.(string)
gcpConnector := connector.New(cidrProviderBucket, data.Get("base_cidr").(string))
networkConfig, err := gcpConnector.ReadRemote(ctx)
Expand Down

0 comments on commit a7c7d87

Please sign in to comment.