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

Add Subnet support #126

Merged
merged 9 commits into from
Oct 24, 2022
Merged

Conversation

haardikdharma10
Copy link
Contributor

@haardikdharma10 haardikdharma10 commented Oct 4, 2022

Description

Adds subnet support.

@haardikdharma10
Copy link
Contributor Author

Tests will fail for time being as API is not ready to receive the requests yet.

@haardikdharma10 haardikdharma10 changed the title Add Subnet support inside Networks Add Subnet support Oct 6, 2022
network.go Outdated
Comment on lines 212 to 232
func (c *Client) FindSubnet(search, networkID string) (*Subnet, error) {
subnets, err := c.ListSubnets(networkID)
if err != nil {
return nil, decodeError(err)
}

exactMatch := false
partialMatchesCount := 0
result := Subnet{}

for _, value := range subnets {
if value.Name == search || value.ID == search || value.Label == search {
exactMatch = true
result = value
} else if strings.Contains(value.Name, search) || strings.Contains(value.ID, search) || strings.Contains(value.Label, search) {
if !exactMatch {
result = value
partialMatchesCount++
}
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have an endpoint to match the exact name: (GET /v2/networks/{networkId}/subnets?name=<exact-name>) so we can use that.
if we want to implement also the partial match I think you can proceed the same way without taking care of the exact match (covered by the previous case I just mentioned), collecting all the matching results and returning them like grep does (instead of returning an error if there are more matches)


// GetSubnet gets a subnet with ID
func (c *Client) GetSubnet(networkID, subnetID string) (*Subnet, error) {
resp, err := c.SendGetRequest(fmt.Sprintf("/v2/networks/%s/subnets/", networkID) + subnetID)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can also do: fmt.Sprintf("/v2/networks/%s/subnets/%s", networkID, subnetID)

Copy link
Member

@alessandroargentieri alessandroargentieri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@alessandroargentieri alessandroargentieri merged commit ea5f891 into civo:master Oct 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants