Skip to content

Commit c45e35b

Browse files
committed
Merge pull request #196 from QuentinPerez/add_ips
Added _ips
2 parents 132048d + 4dd9244 commit c45e35b

File tree

3 files changed

+148
-0
lines changed

3 files changed

+148
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -1131,6 +1131,7 @@ $ scw inspect myserver | jq '.[0].public_ip.address'
11311131

11321132
### master (unreleased)
11331133

1134+
* Support of 'scw _ips' command ... ([#196](https://github.com/scaleway/scaleway-cli/issues/196))
11341135
* Report **permissions** in `scw info` ([#191](https://github.com/scaleway/scaleway-cli/issues/191))
11351136
* Report **dashboard** statistics in `scw info` ([#177](https://github.com/scaleway/scaleway-cli/issues/177))
11361137
* Support of `scw _userdata name VAR=@/path/to/file` ([#183](https://github.com/scaleway/scaleway-cli/issues/183))

pkg/api/api.go

+146
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,25 @@ type ScalewayGetSecurityGroup struct {
434434
SecurityGroups ScalewaySecurityGroups `json:"security_group"`
435435
}
436436

437+
type ScalewayIPDefinition struct {
438+
Organization string `json:"organization"`
439+
Reverse string `json:"reverse"`
440+
ID string `json:"id"`
441+
Server struct {
442+
Identifier string `json:"id,omitempty"`
443+
Name string `json:"name,omitempty"`
444+
} `json:"server,omitempty"`
445+
Address string `json:"address"`
446+
}
447+
448+
type ScalewayGetIPS struct {
449+
IPS []ScalewayIPDefinition `json:"ips"`
450+
}
451+
452+
type ScalewayGetIP struct {
453+
IP ScalewayIPDefinition `json:"ip"`
454+
}
455+
437456
// ScalewaySecurityGroup represents a Scaleway security group
438457
type ScalewaySecurityGroup struct {
439458
// Identifier is a unique identifier for the security group
@@ -1872,6 +1891,133 @@ func (s *ScalewayAPI) GetASecurityGroup(groupsID string) (*ScalewayGetSecurityGr
18721891
return &securityGroups, nil
18731892
}
18741893

1894+
// GetIPS returns a ScalewayGetIPS
1895+
func (s *ScalewayAPI) GetIPS() (*ScalewayGetIPS, error) {
1896+
resp, err := s.GetResponse("ips")
1897+
if err != nil {
1898+
return nil, err
1899+
}
1900+
defer resp.Body.Close()
1901+
1902+
var ips ScalewayGetIPS
1903+
decoder := json.NewDecoder(resp.Body)
1904+
err = decoder.Decode(&ips)
1905+
if err != nil {
1906+
return nil, err
1907+
}
1908+
return &ips, nil
1909+
}
1910+
1911+
// NewIP returns a new IP
1912+
func (s *ScalewayAPI) NewIP() (*ScalewayGetIP, error) {
1913+
var orga struct {
1914+
Organization string `json:"organization"`
1915+
}
1916+
orga.Organization = s.Organization
1917+
resp, err := s.PostResponse("ips", orga)
1918+
if err != nil {
1919+
return nil, err
1920+
}
1921+
defer resp.Body.Close()
1922+
1923+
// Succeed POST code
1924+
if resp.StatusCode == 201 {
1925+
var ip ScalewayGetIP
1926+
decoder := json.NewDecoder(resp.Body)
1927+
err = decoder.Decode(&ip)
1928+
if err != nil {
1929+
return nil, err
1930+
}
1931+
return &ip, nil
1932+
}
1933+
var error ScalewayAPIError
1934+
decoder := json.NewDecoder(resp.Body)
1935+
err = decoder.Decode(&error)
1936+
if err != nil {
1937+
return nil, err
1938+
}
1939+
error.StatusCode = resp.StatusCode
1940+
error.Debug()
1941+
return nil, error
1942+
}
1943+
1944+
// AttachIP attachs an IP to a server
1945+
func (s *ScalewayAPI) AttachIP(ipID, serverID string) error {
1946+
var update struct {
1947+
Address string `json:"address"`
1948+
ID string `json:"id"`
1949+
Reverse *string `json:"reverse"`
1950+
Organization string `json:"organization"`
1951+
Server string `json:"server"`
1952+
}
1953+
1954+
ip, err := s.GetIP(idIP)
1955+
if err != nil {
1956+
return err
1957+
}
1958+
update.Address = ip.IP.Address
1959+
update.ID = ip.IP.ID
1960+
update.Organization = ip.IP.Organization
1961+
update.Server = serverID
1962+
resp, err := s.PutResponse(fmt.Sprintf("ips/%s", ipID), update)
1963+
if err != nil {
1964+
return err
1965+
}
1966+
if resp.StatusCode == 200 {
1967+
return nil
1968+
}
1969+
var error ScalewayAPIError
1970+
decoder := json.NewDecoder(resp.Body)
1971+
err = decoder.Decode(&error)
1972+
if err != nil {
1973+
return err
1974+
}
1975+
error.StatusCode = resp.StatusCode
1976+
error.Debug()
1977+
return error
1978+
}
1979+
1980+
// DeleteIP deletes an IP
1981+
func (s *ScalewayAPI) DeleteIP(ipID string) error {
1982+
resp, err := s.DeleteResponse(fmt.Sprintf("ips/%s", ipID))
1983+
if err != nil {
1984+
return err
1985+
}
1986+
defer resp.Body.Close()
1987+
1988+
// Succeed PUT code
1989+
if resp.StatusCode == 204 {
1990+
return nil
1991+
}
1992+
1993+
var error ScalewayAPIError
1994+
decoder := json.NewDecoder(resp.Body)
1995+
err = decoder.Decode(&error)
1996+
if err != nil {
1997+
return err
1998+
}
1999+
error.StatusCode = resp.StatusCode
2000+
error.Debug()
2001+
return error
2002+
}
2003+
2004+
// GetIP returns a ScalewayGetIP
2005+
func (s *ScalewayAPI) GetIP(ipID string) (*ScalewayGetIP, error) {
2006+
resp, err := s.GetResponse(fmt.Sprintf("ips/%s", ipID))
2007+
if err != nil {
2008+
return nil, err
2009+
}
2010+
defer resp.Body.Close()
2011+
2012+
var ip ScalewayGetIP
2013+
decoder := json.NewDecoder(resp.Body)
2014+
err = decoder.Decode(&ip)
2015+
if err != nil {
2016+
return nil, err
2017+
}
2018+
return &ip, nil
2019+
}
2020+
18752021
// GetBootscriptID returns exactly one bootscript matching or dies
18762022
func (s *ScalewayAPI) GetBootscriptID(needle string) string {
18772023
// Parses optional type prefix, i.e: "bootscript:name" -> "name"

pkg/cli/commands.go

+1
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,5 @@ var Commands = []*Command{
4646
cmdFlushCache,
4747
cmdPatch,
4848
cmdSecurityGroups,
49+
cmdIPS,
4950
}

0 commit comments

Comments
 (0)