Skip to content

Commit 00a3082

Browse files
author
Quentin Perez
committed
adds _security-groups
1 parent 5704b99 commit 00a3082

File tree

5 files changed

+435
-7
lines changed

5 files changed

+435
-7
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,18 +1134,19 @@ $ scw inspect myserver | jq '.[0].public_ip.address'
11341134
* Support of `scw _userdata name VAR=@/path/to/file` ([#183](https://github.com/scaleway/scaleway-cli/issues/183))
11351135
* Support of `scw restart -w` ([#185](https://github.com/scaleway/scaleway-cli/issues/185))
11361136
* Restarting multiple servers in parallel ([#185](https://github.com/scaleway/scaleway-cli/issues/185))
1137+
* Added _security-groups ([#179](https://github.com/scaleway/scaleway-cli/issues/179))
11371138

11381139
View full [commits list](https://github.com/scaleway/scaleway-cli/compare/v1.5.0...master)
11391140

11401141
### v1.5.0 (2015-09-11)
11411142

1142-
* Support of `scw tag --bootscript=""` option ([#149](https://github.com/scaleway/scaleway-cli/issues/149)
1143-
* `scw info` now prints user/organization info from the API ([#130](https://github.com/scaleway/scaleway-cli/issues/130)
1143+
* Support of `scw tag --bootscript=""` option ([#149](https://github.com/scaleway/scaleway-cli/issues/149))
1144+
* `scw info` now prints user/organization info from the API ([#130](https://github.com/scaleway/scaleway-cli/issues/130))
11441145
* Added helpers to manipulate new `user_data` API ([#150](https://github.com/scaleway/scaleway-cli/issues/150))
11451146
* Renamed `create-image-from-s3.sh` example and now auto-filling image metadata (title and bootscript) based on the Makefile configuration
11461147
* Support of `scw rm -f/--force` option ([#158](https://github.com/scaleway/scaleway-cli/issues/158))
11471148
* Added `scw _userdata local ...` option which interacts with the Metadata API without authentication ([#166](https://github.com/scaleway/scaleway-cli/issues/166))
1148-
* Initial version of `scw _billing` (price estimation tool) ([#118](https://github.com/scaleway/scaleway-cli/issues/118)
1149+
* Initial version of `scw _billing` (price estimation tool) ([#118](https://github.com/scaleway/scaleway-cli/issues/118))
11491150
* Fix: debian-package installation
11501151
* Fix: nil pointer dereference ([#155](https://github.com/scaleway/scaleway-cli/pull/155)) ([@ebfe](https://github.com/ebfe))
11511152
* Fix: regression on scw create ([#142](https://github.com/scaleway/scaleway-cli/issues/142))

pkg/api/api.go

Lines changed: 133 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ import (
3030

3131
// Default values
3232
var (
33-
ComputeAPI string = "https://api.scaleway.com/"
34-
AccountAPI string = "https://account.scaleway.com/"
35-
MetadataAPI string = "http://169.254.42.42/"
33+
ComputeAPI = "https://api.scaleway.com/"
34+
AccountAPI = "https://account.scaleway.com/"
35+
MetadataAPI = "http://169.254.42.42/"
3636
)
3737

3838
// ScalewayAPI is the interface used to communicate with the Scaleway API
@@ -381,6 +381,59 @@ type ScalewayTasks struct {
381381
Tasks []ScalewayTask `json:"tasks,omitempty"`
382382
}
383383

384+
// ScalewaySecurityGroupRule definition
385+
type ScalewaySecurityGroupRule struct {
386+
Direction string `json:"direction"`
387+
Protocol string `json:"protocol"`
388+
IPRange string `json:"ip_range"`
389+
DestPortFrom int `json:"dest_port_from,omitempty"`
390+
Action string `json:"action"`
391+
Postion int `json:"position"`
392+
DestPortTo string `json:"dest_port_to"`
393+
Editable bool `json:"editable"`
394+
ID string `json:"id"`
395+
}
396+
397+
// ScalewayGetSecurityGroupRules represents the response of a GET /security_group/{groupID}/rules
398+
type ScalewayGetSecurityGroupRules struct {
399+
Rules []ScalewaySecurityGroupRule `json:"rules"`
400+
}
401+
402+
// ScalewayGetSecurityGroupRule represents the response of a GET /security_group/{groupID}/rules/{ruleID}
403+
type ScalewayGetSecurityGroupRule struct {
404+
Rules ScalewaySecurityGroupRule `json:"rule"`
405+
}
406+
407+
// ScalewayNewSecurityGroupRule definition POST/PUT request /security_group/{groupID}
408+
type ScalewayNewSecurityGroupRule struct {
409+
Action string `json:"action"`
410+
Direction string `json:"direction"`
411+
IPRange string `json:"ip_range"`
412+
Protocol string `json:"protocol"`
413+
DestPortFrom int `json:"dest_port_from,omitempty"`
414+
}
415+
416+
// ScalewaySecurityGroups definition
417+
type ScalewaySecurityGroups struct {
418+
Description string `json:"description"`
419+
EnableDefaultSecurity bool `json:"enable_default_security"`
420+
ID string `json:"id"`
421+
Organization string `json:"organization"`
422+
Name string `json:"name"`
423+
OrganizationDefault bool `json:"organization_default"`
424+
Servers []ScalewaySecurityGroup `json:"servers"`
425+
}
426+
427+
// ScalewayGetSecurityGroups represents the response of a GET /security_groups/
428+
type ScalewayGetSecurityGroups struct {
429+
SecurityGroups []ScalewaySecurityGroups `json:"security_groups"`
430+
}
431+
432+
// ScalewayGetSecurityGroup represents the response of a GET /security_groups/{groupID}
433+
type ScalewayGetSecurityGroup struct {
434+
SecurityGroups ScalewaySecurityGroups `json:"security_group"`
435+
}
436+
384437
// ScalewaySecurityGroup represents a Scaleway security group
385438
type ScalewaySecurityGroup struct {
386439
// Identifier is a unique identifier for the security group
@@ -390,6 +443,13 @@ type ScalewaySecurityGroup struct {
390443
Name string `json:"name,omitempty"`
391444
}
392445

446+
// ScalewayNewSecurityGroup definition POST/PUT request /security_groups
447+
type ScalewayNewSecurityGroup struct {
448+
Organization string `json:"organization"`
449+
Name string `json:"name"`
450+
Description string `json:"description"`
451+
}
452+
393453
// ScalewayServer represents a Scaleway C1 server
394454
type ScalewayServer struct {
395455
// Identifier is a unique identifier for the server
@@ -575,6 +635,7 @@ type ScalewayUserDefinition struct {
575635
SSHPublicKeys []ScalewayKeyDefinition `json:"ssh_public_keys"`
576636
}
577637

638+
// ScalewayUsersDefinition represents the response of a GET /user
578639
type ScalewayUsersDefinition struct {
579640
User ScalewayUserDefinition `json:"user"`
580641
}
@@ -1359,6 +1420,7 @@ func (s *ScalewayAPI) GetBootscript(bootscriptID string) (*ScalewayBootscript, e
13591420
return &oneBootscript.Bootscript, nil
13601421
}
13611422

1423+
// ScalewayUserdatas represents the response of a GET /user_data
13621424
type ScalewayUserdatas struct {
13631425
UserData []string `json:"user_data"`
13641426
}
@@ -1678,6 +1740,74 @@ func (s *ScalewayAPI) GetImageID(needle string, exitIfMissing bool) string {
16781740
return ""
16791741
}
16801742

1743+
// GetSecurityGroups returns a ScalewaySecurityGroups
1744+
func (s *ScalewayAPI) GetSecurityGroups() (*ScalewayGetSecurityGroups, error) {
1745+
resp, err := s.GetResponse("security_groups")
1746+
if err != nil {
1747+
return nil, err
1748+
}
1749+
defer resp.Body.Close()
1750+
1751+
var securityGroups ScalewayGetSecurityGroups
1752+
decoder := json.NewDecoder(resp.Body)
1753+
err = decoder.Decode(&securityGroups)
1754+
if err != nil {
1755+
return nil, err
1756+
}
1757+
return &securityGroups, nil
1758+
}
1759+
1760+
// GetSecurityGroupRules returns a ScalewaySecurityGroupRules
1761+
func (s *ScalewayAPI) GetSecurityGroupRules(groupID string) (*ScalewayGetSecurityGroupRules, error) {
1762+
resp, err := s.GetResponse(fmt.Sprintf("security_groups/%s/rules", groupID))
1763+
if err != nil {
1764+
return nil, err
1765+
}
1766+
defer resp.Body.Close()
1767+
1768+
var securityGroupRules ScalewayGetSecurityGroupRules
1769+
decoder := json.NewDecoder(resp.Body)
1770+
err = decoder.Decode(&securityGroupRules)
1771+
if err != nil {
1772+
return nil, err
1773+
}
1774+
return &securityGroupRules, nil
1775+
}
1776+
1777+
// GetASecurityGroupRule returns a ScalewaySecurityGroupRule
1778+
func (s *ScalewayAPI) GetASecurityGroupRule(groupID string, rulesID string) (*ScalewayGetSecurityGroupRule, error) {
1779+
resp, err := s.GetResponse(fmt.Sprintf("security_groups/%s/rules/%s", groupID, rulesID))
1780+
if err != nil {
1781+
return nil, err
1782+
}
1783+
defer resp.Body.Close()
1784+
1785+
var securityGroupRules ScalewayGetSecurityGroupRule
1786+
decoder := json.NewDecoder(resp.Body)
1787+
err = decoder.Decode(&securityGroupRules)
1788+
if err != nil {
1789+
return nil, err
1790+
}
1791+
return &securityGroupRules, nil
1792+
}
1793+
1794+
// GetASecurityGroup returns a ScalewaySecurityGroup
1795+
func (s *ScalewayAPI) GetASecurityGroup(groupsID string) (*ScalewayGetSecurityGroup, error) {
1796+
resp, err := s.GetResponse(fmt.Sprintf("security_groups/%s", groupsID))
1797+
if err != nil {
1798+
return nil, err
1799+
}
1800+
defer resp.Body.Close()
1801+
1802+
var securityGroups ScalewayGetSecurityGroup
1803+
decoder := json.NewDecoder(resp.Body)
1804+
err = decoder.Decode(&securityGroups)
1805+
if err != nil {
1806+
return nil, err
1807+
}
1808+
return &securityGroups, nil
1809+
}
1810+
16811811
// GetBootscriptID returns exactly one bootscript matching or dies
16821812
func (s *ScalewayAPI) GetBootscriptID(needle string) string {
16831813
// Parses optional type prefix, i.e: "bootscript:name" -> "name"

pkg/cli/commands.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,5 @@ var Commands = []*Command{
4545
cmdCompletion,
4646
cmdFlushCache,
4747
cmdPatch,
48+
cmdSecurityGroups,
4849
}

pkg/cli/x_patch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func runPatch(cmd *Command, args []string) error {
4343
// Parsing FIELD=VALUE
4444
updateParts := strings.SplitN(args[1], "=", 2)
4545
if len(updateParts) != 2 {
46-
cmd.PrintShortUsage()
46+
return cmd.PrintShortUsage()
4747
}
4848
fieldName := updateParts[0]
4949
newValue := updateParts[1]

0 commit comments

Comments
 (0)