Skip to content
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

Get IPAddress if resource already has IP assigned #86

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
131 changes: 131 additions & 0 deletions internal/graphapi/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions internal/graphapi/ipaddress.resolvers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion internal/graphapi/ipaddress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,12 @@ func Test_IPAddress_Lifecycle(t *testing.T) {

ipbt := (&IPBlockTypeBuilder{}).MustNew(ctx)
ipb := (&IPBlockBuilder{IPBlockTypeID: ipbt.ID}).MustNew(ctx)
node := gidx.MustNewID(nodePrefix)

t.Run("Create", func(t *testing.T) {
ipa, err := client.CreateIPAddress(ctx, testclient.CreateIPAddressInput{
IP: gofakeit.IPv4Address(),
NodeID: gidx.MustNewID(nodePrefix),
NodeID: node,
NodeOwnerID: gidx.MustNewID(ownerPrefix),
Reserved: newBool(true),
IPBlockID: ipb.ID,
Expand All @@ -90,6 +91,12 @@ func Test_IPAddress_Lifecycle(t *testing.T) {
require.NotNil(t, ipaUpdate)
require.Equal(t, false, ipaUpdate.UpdateIPAddress.IPAddress.Reserved)

addr, err := client.GetIPAddressByNode(ctx, node)

require.NoError(t, err)
require.NotNil(t, addr)
require.Equal(t, addr.IPAddressByNode.ID, ipa.CreateIPAddress.IPAddress.ID)

ipaDelete, err := client.DeleteIPAddress(ctx, ipa.CreateIPAddress.IPAddress.ID)

require.NoError(t, err)
Expand Down
53 changes: 48 additions & 5 deletions internal/testclient/gen_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions internal/testclient/ipaddress.gql
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,20 @@ query getIPAddress($id: ID!) {
}
}

query getIPAddressByNode($id: ID!) {
ipAddressByNode(id: $id) {
id
ip
reserved
ipBlock {
id
prefix
allowAutoAllocate
allowAutoSubnet
}
}
}

mutation CreateIPAddress($input: CreateIPAddressInput!) {
createIPAddress(input: $input) {
ipAddress {
Expand Down
5 changes: 5 additions & 0 deletions internal/testclient/schema/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,11 @@ type Query {
"""ID of the ip address"""
id: ID!
): IPAddress!
"""Look up ip address by Node ID"""
ipAddressByNode(
"""ID of the Node"""
id: ID!
): IPAddress!
"""Look up ip block by ID"""
ipBlock(
"""ID of the ip block"""
Expand Down
19 changes: 19 additions & 0 deletions pkg/ipamclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,22 @@ func (c *Client) GetIPAddresses(ctx context.Context, nodeID string) ([]IPAddress

return nodeIPs.NodeIPAddress.LoadBalancerFragment.IPAddresses, nil
}

// GetIPAddressByNode returns an IP Address by node id
func (c *Client) GetIPAddressByNode(ctx context.Context, nodeID string) (*GetIPAddressByNode, error) {
_, err := gidx.Parse(nodeID)
if err != nil {
return nil, err
}

vars := map[string]interface{}{
"id": graphql.ID(nodeID),
}

var nodeIP GetIPAddressByNode
if err := c.gqlCli.Query(ctx, &nodeIP, vars); err != nil {
return nil, err
}

return &nodeIP, nil
}
27 changes: 27 additions & 0 deletions pkg/ipamclient/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,33 @@ func TestGetIPAddressesByNodeID(t *testing.T) {
})
}

func TestGetIPAddressByNode(t *testing.T) {
cli := Client{
gqlCli: mustNewGQLTestClient(
`{
"data": {
"ipAddressByNode": {
"id": "ipamipa-12345",
"ip": "192.168.10.1",
"reserved": false
}
}
}`),
}

ipResult, err := cli.GetIPAddressByNode(context.Background(), "nodeids-test")
require.NoError(t, err)
require.NotNil(t, ipResult)

assert.Equal(t, ipResult.IPAddress.ID, "ipamipa-12345")
assert.Equal(t, ipResult.IPAddress.IP, "192.168.10.1")
assert.False(t, ipResult.IPAddress.Reserved)

baddress, err := cli.GetIPAddressByNode(context.TODO(), "badprefix-test")
require.Error(t, err)
require.Nil(t, baddress)
}

func mustNewGQLTestClient(respJSON string) *graphql.Client {
mux := http.NewServeMux()
mux.HandleFunc("/query", func(w http.ResponseWriter, req *http.Request) {
Expand Down
5 changes: 5 additions & 0 deletions pkg/ipamclient/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,8 @@ type GetIPAddressesByNode struct {
} `graphql:"... on LoadBalancer"`
} `graphql:"node(id: $id)"`
}

// GetIPAddressByNode is the query used for getting an IP Address
type GetIPAddressByNode struct {
IPAddress GetIPAddressResult `graphql:"ipAddressByNode(id: $id)"`
}
Loading
Loading