Skip to content

Commit 7a57ff4

Browse files
committed
Merge pull request #245 from QuentinPerez/fix_197
Refactor securityGroups
2 parents 695737b + c36f8ea commit 7a57ff4

File tree

3 files changed

+241
-260
lines changed

3 files changed

+241
-260
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -1146,6 +1146,7 @@ $ scw inspect myserver | jq '.[0].public_ip.address'
11461146
### master (unreleased)
11471147

11481148
* Support of `scw run --userdata=...` ([#202](https://github.com/scaleway/scaleway-cli/issues/202))
1149+
* Refactor of `scw _security-groups` ([#197](https://github.com/scaleway/scaleway-cli/issues/197))
11491150
* Support of `scw tag --arch=XXX`
11501151
* Support of `scw run --timeout=X` ([#239](https://github.com/scaleway/scaleway-cli/issues/239))
11511152
* Check the "stopped" state for `scw run | exec -w`([#229](https://github.com/scaleway/scaleway-cli/issues/229))

pkg/api/api.go

+80-17
Original file line numberDiff line numberDiff line change
@@ -1040,10 +1040,8 @@ func (s *ScalewayAPI) PostServerAction(serverID, action string) error {
10401040
}
10411041
defer resp.Body.Close()
10421042

1043-
if _, err = s.handleHTTPError([]int{202}, resp); err != nil {
1044-
return err
1045-
}
1046-
return nil
1043+
_, err = s.handleHTTPError([]int{202}, resp)
1044+
return err
10471045
}
10481046

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

1208-
if _, err := s.handleHTTPError([]int{200}, resp); err != nil {
1209-
return err
1210-
}
1211-
return nil
1206+
_, err = s.handleHTTPError([]int{200}, resp)
1207+
return err
12121208
}
12131209

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

1578-
// Succeed POST code
1579-
if resp.StatusCode == 204 {
1580-
return nil
1581-
}
1582-
return fmt.Errorf("cannot delete user_data (%d)", resp.StatusCode)
1574+
_, err = s.handleHTTPError([]int{204}, resp)
1575+
return err
15831576
}
15841577

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

1892+
// PostSecurityGroup posts a group on a server
1893+
func (s *ScalewayAPI) PostSecurityGroup(group ScalewayNewSecurityGroup) error {
1894+
resp, err := s.PostResponse("security_groups", group)
1895+
if err != nil {
1896+
return err
1897+
}
1898+
defer resp.Body.Close()
1899+
1900+
_, err = s.handleHTTPError([]int{201}, resp)
1901+
return err
1902+
}
1903+
1904+
// PostSecurityGroupRule posts a rule on a server
1905+
func (s *ScalewayAPI) PostSecurityGroupRule(SecurityGroupID string, rules ScalewayNewSecurityGroupRule) error {
1906+
resp, err := s.PostResponse(fmt.Sprintf("security_groups/%s/rules", SecurityGroupID), rules)
1907+
if err != nil {
1908+
return err
1909+
}
1910+
defer resp.Body.Close()
1911+
1912+
_, err = s.handleHTTPError([]int{201}, resp)
1913+
return err
1914+
}
1915+
1916+
// DeleteSecurityGroup deletes a SecurityGroup
1917+
func (s *ScalewayAPI) DeleteSecurityGroup(securityGroupID string) error {
1918+
resp, err := s.DeleteResponse(fmt.Sprintf("security_groups/%s", securityGroupID))
1919+
if err != nil {
1920+
return err
1921+
}
1922+
defer resp.Body.Close()
1923+
1924+
_, err = s.handleHTTPError([]int{204}, resp)
1925+
return err
1926+
}
1927+
1928+
// PutSecurityGroup updates a SecurityGroup
1929+
func (s *ScalewayAPI) PutSecurityGroup(group ScalewayNewSecurityGroup, securityGroupID string) error {
1930+
resp, err := s.PutResponse(fmt.Sprintf("security_groups/%s", securityGroupID), group)
1931+
if err != nil {
1932+
return err
1933+
}
1934+
defer resp.Body.Close()
1935+
1936+
_, err = s.handleHTTPError([]int{200}, resp)
1937+
return err
1938+
}
1939+
1940+
// PutSecurityGroupRule updates a SecurityGroupRule
1941+
func (s *ScalewayAPI) PutSecurityGroupRule(rules ScalewayNewSecurityGroupRule, securityGroupID, RuleID string) error {
1942+
resp, err := s.PutResponse(fmt.Sprintf("security_groups/%s/rules/%s", securityGroupID, RuleID), rules)
1943+
if err != nil {
1944+
return err
1945+
}
1946+
defer resp.Body.Close()
1947+
1948+
_, err = s.handleHTTPError([]int{200}, resp)
1949+
return err
1950+
}
1951+
1952+
// DeleteSecurityGroupRule deletes a SecurityGroupRule
1953+
func (s *ScalewayAPI) DeleteSecurityGroupRule(SecurityGroupID, RuleID string) error {
1954+
resp, err := s.DeleteResponse(fmt.Sprintf("security_groups/%s/rules/%s", SecurityGroupID, RuleID))
1955+
if err != nil {
1956+
return err
1957+
}
1958+
defer resp.Body.Close()
1959+
1960+
_, err = s.handleHTTPError([]int{204}, resp)
1961+
return err
1962+
}
1963+
18991964
// GetContainers returns a ScalewayGetContainers
19001965
func (s *ScalewayAPI) GetContainers() (*ScalewayGetContainers, error) {
19011966
resp, err := s.GetResponse("containers")
@@ -2002,10 +2067,8 @@ func (s *ScalewayAPI) AttachIP(ipID, serverID string) error {
20022067
if err != nil {
20032068
return err
20042069
}
2005-
if _, err := s.handleHTTPError([]int{200}, resp); err != nil {
2006-
return err
2007-
}
2008-
return nil
2070+
_, err = s.handleHTTPError([]int{200}, resp)
2071+
return err
20092072
}
20102073

20112074
// DeleteIP deletes an IP

0 commit comments

Comments
 (0)