Skip to content

Commit bc808cc

Browse files
Share IP allow-list request parsing (#438)
We already have a shared utility to parse a single `--ip-allow-list` value. This factors logic out of the keyvalue package to parse the result when a user supplies multiple. GitOrigin-RevId: f02d85f3c874752093f3453a5e9002656d5bec01
1 parent 5d962df commit bc808cc

5 files changed

Lines changed: 53 additions & 27 deletions

File tree

pkg/keyvalue/create.go

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ func BuildCreateRequest(input kvtypes.KeyValueCreateRequestInput) (client.Create
129129
}
130130

131131
if len(input.IPAllowList) > 0 {
132-
entries, err := parseIPAllowList(input.IPAllowList)
132+
entries, err := types.ParseIPAllowList(input.IPAllowList)
133133
if err != nil {
134134
return client.CreateKeyValueJSONRequestBody{}, err
135135
}
@@ -138,18 +138,3 @@ func BuildCreateRequest(input kvtypes.KeyValueCreateRequestInput) (client.Create
138138

139139
return body, nil
140140
}
141-
142-
func parseIPAllowList(raw []string) ([]client.CidrBlockAndDescription, error) {
143-
out := make([]client.CidrBlockAndDescription, 0, len(raw))
144-
for _, entry := range raw {
145-
cidr, desc, err := types.ParseIPAllowListEntry(entry)
146-
if err != nil {
147-
return nil, err
148-
}
149-
out = append(out, client.CidrBlockAndDescription{
150-
CidrBlock: cidr,
151-
Description: desc,
152-
})
153-
}
154-
return out, nil
155-
}

pkg/keyvalue/update.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55

66
"github.com/render-oss/cli/pkg/client"
7+
"github.com/render-oss/cli/pkg/types"
78
kvtypes "github.com/render-oss/cli/pkg/types/keyvalue"
89
)
910

@@ -82,7 +83,7 @@ func BuildUpdateRequest(input kvtypes.KeyValueUpdateInput) (client.UpdateKeyValu
8283
}
8384

8485
if len(input.IPAllowList) > 0 {
85-
entries, err := parseIPAllowList(input.IPAllowList)
86+
entries, err := types.ParseIPAllowList(input.IPAllowList)
8687
if err != nil {
8788
return client.UpdateKeyValueJSONRequestBody{}, err
8889
}

pkg/service/create.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -374,16 +374,9 @@ func ParseIPAllowListInputs(raw []string) (*[]client.CidrBlockAndDescription, er
374374
if len(raw) == 0 {
375375
return nil, nil
376376
}
377-
entries := make([]client.CidrBlockAndDescription, 0, len(raw))
378-
for _, entry := range raw {
379-
cidr, description, err := types.ParseIPAllowListEntry(entry)
380-
if err != nil {
381-
return nil, err
382-
}
383-
entries = append(entries, client.CidrBlockAndDescription{
384-
CidrBlock: cidr,
385-
Description: description,
386-
})
377+
entries, err := types.ParseIPAllowList(raw)
378+
if err != nil {
379+
return nil, err
387380
}
388381
return &entries, nil
389382
}

pkg/types/ipallowlist.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"fmt"
55
"net"
66
"strings"
7+
8+
"github.com/render-oss/cli/pkg/client"
79
)
810

911
// ParseIPAllowListEntry parses an AWS-style composite flag value into a CIDR block and description.
@@ -43,6 +45,24 @@ func ParseIPAllowListEntry(raw string) (cidrBlock string, description string, er
4345
return cidrBlock, description, nil
4446
}
4547

48+
// ParseIPAllowList parses a list of --ip-allow-list flag values into the REST
49+
// API shape. Each entry follows the format documented on
50+
// ParseIPAllowListEntry.
51+
func ParseIPAllowList(raw []string) ([]client.CidrBlockAndDescription, error) {
52+
out := make([]client.CidrBlockAndDescription, 0, len(raw))
53+
for _, entry := range raw {
54+
cidr, desc, err := ParseIPAllowListEntry(entry)
55+
if err != nil {
56+
return nil, err
57+
}
58+
out = append(out, client.CidrBlockAndDescription{
59+
CidrBlock: cidr,
60+
Description: desc,
61+
})
62+
}
63+
return out, nil
64+
}
65+
4666
// FormatIPAllowListEntry formats a CIDR block and description into the composite flag format.
4767
func FormatIPAllowListEntry(cidrBlock, description string) string {
4868
if description != "" {

pkg/types/ipallowlist_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,33 @@ func TestParseIPAllowListEntry(t *testing.T) {
6161
})
6262
}
6363

64+
func TestParseIPAllowList(t *testing.T) {
65+
t.Run("parses each entry into a CidrBlockAndDescription", func(t *testing.T) {
66+
out, err := types.ParseIPAllowList([]string{
67+
"cidr=10.0.0.0/8,description=Internal",
68+
"cidr=203.0.113.5/32",
69+
})
70+
require.NoError(t, err)
71+
require.Len(t, out, 2)
72+
assert.Equal(t, "10.0.0.0/8", out[0].CidrBlock)
73+
assert.Equal(t, "Internal", out[0].Description)
74+
assert.Equal(t, "203.0.113.5/32", out[1].CidrBlock)
75+
assert.Equal(t, "", out[1].Description)
76+
})
77+
78+
t.Run("propagates entry-level errors", func(t *testing.T) {
79+
_, err := types.ParseIPAllowList([]string{"malformed"})
80+
require.Error(t, err)
81+
assert.Contains(t, err.Error(), "must start with cidr=")
82+
})
83+
84+
t.Run("nil input yields empty slice", func(t *testing.T) {
85+
out, err := types.ParseIPAllowList(nil)
86+
require.NoError(t, err)
87+
assert.Empty(t, out)
88+
})
89+
}
90+
6491
func TestFormatIPAllowListEntry(t *testing.T) {
6592
t.Run("formats cidr with description", func(t *testing.T) {
6693
result := types.FormatIPAllowListEntry("10.0.0.0/8", "Internal")

0 commit comments

Comments
 (0)