-
Notifications
You must be signed in to change notification settings - Fork 26
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
Add Subnet support #126
Conversation
Tests will fail for time being as API is not ready to receive the requests yet. |
network.go
Outdated
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++ | ||
} | ||
} | ||
} |
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.
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) |
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.
you can also do: fmt.Sprintf("/v2/networks/%s/subnets/%s", networkID, subnetID)
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.
LGTM
Description
Adds subnet support.