Skip to content

Refactor securityGroups #245

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

Merged
merged 4 commits into from
Dec 17, 2015
Merged
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1146,6 +1146,7 @@ $ scw inspect myserver | jq '.[0].public_ip.address'
### master (unreleased)

* Support of `scw run --userdata=...` ([#202](https://github.com/scaleway/scaleway-cli/issues/202))
* Refactor of `scw _security-groups` ([#197](https://github.com/scaleway/scaleway-cli/issues/197))
* Support of `scw tag --arch=XXX`
* Support of `scw run --timeout=X` ([#239](https://github.com/scaleway/scaleway-cli/issues/239))
* Check the "stopped" state for `scw run | exec -w`([#229](https://github.com/scaleway/scaleway-cli/issues/229))
Expand Down
97 changes: 80 additions & 17 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1040,10 +1040,8 @@ func (s *ScalewayAPI) PostServerAction(serverID, action string) error {
}
defer resp.Body.Close()

if _, err = s.handleHTTPError([]int{202}, resp); err != nil {
return err
}
return nil
_, err = s.handleHTTPError([]int{202}, resp)
return err
}

// DeleteServer deletes a server
Expand Down Expand Up @@ -1205,10 +1203,8 @@ func (s *ScalewayAPI) PutVolume(volumeID string, definition ScalewayVolumePutDef
}
defer resp.Body.Close()

if _, err := s.handleHTTPError([]int{200}, resp); err != nil {
return err
}
return nil
_, err = s.handleHTTPError([]int{200}, resp)
return err
}

// ResolveServer attempts the find a matching Identifier for the input string
Expand Down Expand Up @@ -1575,11 +1571,8 @@ func (s *ScalewayAPI) DeleteUserdata(serverID string, key string) error {
}
defer resp.Body.Close()

// Succeed POST code
if resp.StatusCode == 204 {
return nil
}
return fmt.Errorf("cannot delete user_data (%d)", resp.StatusCode)
_, err = s.handleHTTPError([]int{204}, resp)
return err
}

// GetTasks get the list of tasks from the ScalewayAPI
Expand Down Expand Up @@ -1896,6 +1889,78 @@ func (s *ScalewayAPI) GetASecurityGroup(groupsID string) (*ScalewayGetSecurityGr
return &securityGroups, nil
}

// PostSecurityGroup posts a group on a server
func (s *ScalewayAPI) PostSecurityGroup(group ScalewayNewSecurityGroup) error {
resp, err := s.PostResponse("security_groups", group)
if err != nil {
return err
}
defer resp.Body.Close()

_, err = s.handleHTTPError([]int{201}, resp)
return err
}

// PostSecurityGroupRule posts a rule on a server
func (s *ScalewayAPI) PostSecurityGroupRule(SecurityGroupID string, rules ScalewayNewSecurityGroupRule) error {
resp, err := s.PostResponse(fmt.Sprintf("security_groups/%s/rules", SecurityGroupID), rules)
if err != nil {
return err
}
defer resp.Body.Close()

_, err = s.handleHTTPError([]int{201}, resp)
return err
}

// DeleteSecurityGroup deletes a SecurityGroup
func (s *ScalewayAPI) DeleteSecurityGroup(securityGroupID string) error {
resp, err := s.DeleteResponse(fmt.Sprintf("security_groups/%s", securityGroupID))
if err != nil {
return err
}
defer resp.Body.Close()

_, err = s.handleHTTPError([]int{204}, resp)
return err
}

// PutSecurityGroup updates a SecurityGroup
func (s *ScalewayAPI) PutSecurityGroup(group ScalewayNewSecurityGroup, securityGroupID string) error {
resp, err := s.PutResponse(fmt.Sprintf("security_groups/%s", securityGroupID), group)
if err != nil {
return err
}
defer resp.Body.Close()

_, err = s.handleHTTPError([]int{200}, resp)
return err
}

// PutSecurityGroupRule updates a SecurityGroupRule
func (s *ScalewayAPI) PutSecurityGroupRule(rules ScalewayNewSecurityGroupRule, securityGroupID, RuleID string) error {
resp, err := s.PutResponse(fmt.Sprintf("security_groups/%s/rules/%s", securityGroupID, RuleID), rules)
if err != nil {
return err
}
defer resp.Body.Close()

_, err = s.handleHTTPError([]int{200}, resp)
return err
}

// DeleteSecurityGroupRule deletes a SecurityGroupRule
func (s *ScalewayAPI) DeleteSecurityGroupRule(SecurityGroupID, RuleID string) error {
resp, err := s.DeleteResponse(fmt.Sprintf("security_groups/%s/rules/%s", SecurityGroupID, RuleID))
if err != nil {
return err
}
defer resp.Body.Close()

_, err = s.handleHTTPError([]int{204}, resp)
return err
}

// GetContainers returns a ScalewayGetContainers
func (s *ScalewayAPI) GetContainers() (*ScalewayGetContainers, error) {
resp, err := s.GetResponse("containers")
Expand Down Expand Up @@ -2002,10 +2067,8 @@ func (s *ScalewayAPI) AttachIP(ipID, serverID string) error {
if err != nil {
return err
}
if _, err := s.handleHTTPError([]int{200}, resp); err != nil {
return err
}
return nil
_, err = s.handleHTTPError([]int{200}, resp)
return err
}

// DeleteIP deletes an IP
Expand Down
Loading