diff --git a/builder/profitbricks/config.go b/builder/profitbricks/config.go index 62ffc18c8..bb4e8d37f 100644 --- a/builder/profitbricks/config.go +++ b/builder/profitbricks/config.go @@ -76,7 +76,7 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) { } if c.PBUrl == "" { - c.PBUrl = "https://api.profitbricks.com/rest/v2" + c.PBUrl = "https://api.profitbricks.com/cloudapi/v4" } if c.Cores == 0 { diff --git a/builder/profitbricks/step_create_server.go b/builder/profitbricks/step_create_server.go index 70c9148d5..a4de3cb19 100644 --- a/builder/profitbricks/step_create_server.go +++ b/builder/profitbricks/step_create_server.go @@ -26,34 +26,33 @@ func (s *stepCreateServer) Run(state multistep.StateBag) multistep.StepAction { } ui.Say("Creating Virtual Data Center...") img := s.getImageId(c.Image, c) + alias := "" + if img == "" { + alias = s.getImageAlias(c.Image, c.Region, ui) + } datacenter := profitbricks.Datacenter{ Properties: profitbricks.DatacenterProperties{ Name: c.SnapshotName, Location: c.Region, }, - Entities: profitbricks.DatacenterEntities{ - Servers: &profitbricks.Servers{ - Items: []profitbricks.Server{ + } + server := profitbricks.Server{ + Properties: profitbricks.ServerProperties{ + Name: c.SnapshotName, + Ram: c.Ram, + Cores: c.Cores, + }, + Entities: &profitbricks.ServerEntities{ + Volumes: &profitbricks.Volumes{ + Items: []profitbricks.Volume{ { - Properties: profitbricks.ServerProperties{ - Name: c.SnapshotName, - Ram: c.Ram, - Cores: c.Cores, - }, - Entities: &profitbricks.ServerEntities{ - Volumes: &profitbricks.Volumes{ - Items: []profitbricks.Volume{ - { - Properties: profitbricks.VolumeProperties{ - Type: c.DiskType, - Size: c.DiskSize, - Name: c.SnapshotName, - Image: img, - }, - }, - }, - }, + Properties: profitbricks.VolumeProperties{ + Type: c.DiskType, + Size: c.DiskSize, + Name: c.SnapshotName, + ImageAlias: alias, + Image: img, }, }, }, @@ -61,11 +60,11 @@ func (s *stepCreateServer) Run(state multistep.StateBag) multistep.StepAction { }, } if c.SSHKey != "" { - datacenter.Entities.Servers.Items[0].Entities.Volumes.Items[0].Properties.SshKeys = []string{c.SSHKey} + server.Entities.Volumes.Items[0].Properties.SshKeys = []string{c.SSHKey} } if c.Comm.SSHPassword != "" { - datacenter.Entities.Servers.Items[0].Entities.Volumes.Items[0].Properties.ImagePassword = c.Comm.SSHPassword + server.Entities.Volumes.Items[0].Properties.ImagePassword = c.Comm.SSHPassword } datacenter = profitbricks.CompositeCreateDatacenter(datacenter) @@ -94,8 +93,20 @@ func (s *stepCreateServer) Run(state multistep.StateBag) multistep.StepAction { state.Put("datacenter_id", datacenter.Id) - lan := profitbricks.CreateLan(datacenter.Id, profitbricks.Lan{ - Properties: profitbricks.LanProperties{ + server = profitbricks.CreateServer(datacenter.Id, server) + if server.StatusCode > 299 { + ui.Error(fmt.Sprintf("Error occurred %s", parseErrorMessage(server.Response))) + return multistep.ActionHalt + } + + err = s.waitTillProvisioned(server.Headers.Get("Location"), *c) + if err != nil { + ui.Error(fmt.Sprintf("Error occurred while creating a server %s", err.Error())) + return multistep.ActionHalt + } + + lan := profitbricks.CreateLan(datacenter.Id, profitbricks.CreateLanRequest{ + Properties: profitbricks.CreateLanProperties{ Public: true, Name: c.SnapshotName, }, @@ -113,8 +124,8 @@ func (s *stepCreateServer) Run(state multistep.StateBag) multistep.StepAction { } lanId, _ := strconv.Atoi(lan.Id) - nic := profitbricks.CreateNic(datacenter.Id, datacenter.Entities.Servers.Items[0].Id, profitbricks.Nic{ - Properties: profitbricks.NicProperties{ + nic := profitbricks.CreateNic(datacenter.Id, server.Id, profitbricks.Nic{ + Properties: &profitbricks.NicProperties{ Name: c.SnapshotName, Lan: lanId, Dhcp: true, @@ -132,9 +143,9 @@ func (s *stepCreateServer) Run(state multistep.StateBag) multistep.StepAction { return multistep.ActionHalt } - state.Put("volume_id", datacenter.Entities.Servers.Items[0].Entities.Volumes.Items[0].Id) + state.Put("volume_id", server.Entities.Volumes.Items[0].Id) - server := profitbricks.GetServer(datacenter.Id, datacenter.Entities.Servers.Items[0].Id) + server = profitbricks.GetServer(datacenter.Id, server.Id) state.Put("server_ip", server.Entities.Nics.Items[0].Properties.Ips[0]) @@ -225,6 +236,25 @@ func (d *stepCreateServer) getImageId(imageName string, c *Config) string { return "" } +func (d *stepCreateServer) getImageAlias(imageAlias string, location string, ui packer.Ui) string { + if imageAlias == "" { + return "" + } + locations := profitbricks.GetLocation(location) + if len(locations.Properties.ImageAliases) > 0 { + for _, i := range locations.Properties.ImageAliases { + alias := "" + if i != "" { + alias = i + } + if alias != "" && strings.ToLower(alias) == strings.ToLower(imageAlias) { + return alias + } + } + } + return "" +} + func parseErrorMessage(raw string) (toreturn string) { var tmp map[string]interface{} json.Unmarshal([]byte(raw), &tmp) diff --git a/builder/profitbricks/step_take_snapshot.go b/builder/profitbricks/step_take_snapshot.go index db63df73c..822ea8699 100644 --- a/builder/profitbricks/step_take_snapshot.go +++ b/builder/profitbricks/step_take_snapshot.go @@ -22,7 +22,7 @@ func (s *stepTakeSnapshot) Run(state multistep.StateBag) multistep.StepAction { dcId := state.Get("datacenter_id").(string) volumeId := state.Get("volume_id").(string) - snapshot := profitbricks.CreateSnapshot(dcId, volumeId, c.SnapshotName) + snapshot := profitbricks.CreateSnapshot(dcId, volumeId, c.SnapshotName, "") state.Put("snapshotname", c.SnapshotName) diff --git a/vendor/github.com/profitbricks/profitbricks-sdk-go/.gitignore b/vendor/github.com/profitbricks/profitbricks-sdk-go/.gitignore deleted file mode 100644 index 75eab1e65..000000000 --- a/vendor/github.com/profitbricks/profitbricks-sdk-go/.gitignore +++ /dev/null @@ -1,33 +0,0 @@ -# Compiled Object files, Static and Dynamic libs (Shared Objects) -*.o -*.a -*.so - -# Folders -_obj -_test - -# Architecture specific extensions/prefixes -*.[568vq] -[568vq].out - -*.cgo1.go -*.cgo2.c -_cgo_defun.c -_cgo_gotypes.go -_cgo_export.* - -_testmain.go - -*.exe -*.test -*.prof -/.idea/ -.idea/compiler.xml -.idea/copyright/ -.idea/encodings.xml -.idea/misc.xml -.idea/modules.xml -.idea/profitbricks-sdk-go.iml -.idea/vcs.xml -.idea/workspace.xml diff --git a/vendor/github.com/profitbricks/profitbricks-sdk-go/README.md b/vendor/github.com/profitbricks/profitbricks-sdk-go/README.md index b41efd679..a36630aea 100644 --- a/vendor/github.com/profitbricks/profitbricks-sdk-go/README.md +++ b/vendor/github.com/profitbricks/profitbricks-sdk-go/README.md @@ -1,342 +1,2270 @@ -# Go SDK - -The ProfitBricks Client Library for [Go](https://www.golang.org/) provides you with access to the ProfitBricks REST API. It is designed for developers who are building applications in Go. - -This guide will walk you through getting setup with the library and performing various actions against the API. - -# Table of Contents -* [Concepts](#concepts) -* [Getting Started](#getting-started) -* [Installation](#installation) -* [How to: Create Data Center](#how-to-create-data-center) -* [How to: Delete Data Center](#how-to-delete-data-center) -* [How to: Create Server](#how-to-create-server) -* [How to: List Available Images](#how-to-list-available-images) -* [How to: Create Storage Volume](#how-to-create-storage-volume) -* [How to: Update Cores and Memory](#how-to-update-cores-and-memory) -* [How to: Attach or Detach Storage Volume](#how-to-attach-or-detach-storage-volume) -* [How to: List Servers, Volumes, and Data Centers](#how-to-list-servers-volumes-and-data-centers) -* [Example](#example) -* [Return Types](#return-types) -* [Support](#support) - - -# Concepts - -The Go SDK wraps the latest version of the ProfitBricks REST API. All API operations are performed over SSL and authenticated using your ProfitBricks portal credentials. The API can be accessed within an instance running in ProfitBricks or directly over the Internet from any application that can send an HTTPS request and receive an HTTPS response. - -# Getting Started - -Before you begin you will need to have [signed-up](https://www.profitbricks.com/signup) for a ProfitBricks account. The credentials you setup during sign-up will be used to authenticate against the API. - -Install the Go language from: [Go Installation](https://golang.org/doc/install) - -The `GOPATH` environment variable specifies the location of your Go workspace. It is likely the only environment variable you'll need to set when developing Go code. This is an example of pointing to a workspace configured underneath your home directory: - -``` -mkdir -p ~/go/bin -export GOPATH=~/go -export GOBIN=$GOPATH/bin -export PATH=$PATH:$GOBIN -``` - -# Installation - -The following go command will download `profitbricks-sdk-go` to your configured `GOPATH`: - -```go -go get "github.com/profitbricks/profitbricks-sdk-go" -``` - -The source code of the package will be located at: - - $GOBIN\src\profitbricks-sdk-go - -Create main package file *example.go*: - -```go -package main - -import ( - "fmt" -) - -func main() { -} -``` - -Import GO SDK: - -```go -import( - "github.com/profitbricks/profitbricks-sdk-go" -) -``` - -Add your credentials for connecting to ProfitBricks: - -```go -profitbricks.SetAuth("username", "password") -``` - -Set depth: - -```go -profitbricks.SetDepth("5") -``` - -Depth controls the amount of data returned from the REST server ( range 1-5 ). The larger the number the more information is returned from the server. This is especially useful if you are looking for the information in the nested objects. - -**Caution**: You will want to ensure you follow security best practices when using credentials within your code or stored in a file. - -# How To's - -## How To: Create Data Center - -ProfitBricks introduces the concept of Data Centers. These are logically separated from one another and allow you to have a self-contained environment for all servers, volumes, networking, snapshots, and so forth. The goal is to give you the same experience as you would have if you were running your own physical data center. - -The following code example shows you how to programmatically create a data center: - -```go -dcrequest := profitbricks.Datacenter{ - Properties: profitbricks.DatacenterProperties{ - Name: "example.go3", - Description: "description", - Location: "us/lasdev", - }, - } - -datacenter := profitbricks.CreateDatacenter(dcrequest) -``` - -## How To: Create Data Center with Multiple Resources - -To create a complex Data Center you would do this. As you can see, you can create quite a few of the objects you will need later all in one request.: - -```go -datacenter := model.Datacenter{ - Properties: model.DatacenterProperties{ - Name: "composite test", - Location:location, - }, - Entities:model.DatacenterEntities{ - Servers: &model.Servers{ - Items:[]model.Server{ - model.Server{ - Properties: model.ServerProperties{ - Name : "server1", - Ram: 2048, - Cores: 1, - }, - Entities:model.ServerEntities{ - Volumes: &model.AttachedVolumes{ - Items:[]model.Volume{ - model.Volume{ - Properties: model.VolumeProperties{ - Type_:"HDD", - Size:10, - Name:"volume1", - Image:"1f46a4a3-3f47-11e6-91c6-52540005ab80", - Bus:"VIRTIO", - ImagePassword:"test1234", - SshKeys: []string{"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCoLVLHON4BSK3D8L4H79aFo..."}, - }, - }, - }, - }, - Nics: &model.Nics{ - Items: []model.Nic{ - model.Nic{ - Properties: model.NicProperties{ - Name : "nic", - Lan : "1", - }, - }, - }, - }, - }, - }, - }, - }, - }, - } - -dc := CompositeCreateDatacenter(datacenter) - -``` - - -## How To: Delete Data Center - -You will want to exercise a bit of caution here. Removing a data center will destroy all objects contained within that data center -- servers, volumes, snapshots, and so on. - -The code to remove a data center is as follows. This example assumes you want to remove previously data center: - -```go -profitbricks.DeleteDatacenter(response.Id) -``` - -## How To: Create Server - -The server create method has a list of required parameters followed by a hash of optional parameters. The optional parameters are specified within the "options" hash and the variable names match the [REST API](https://devops.profitbricks.com/api/rest/) parameters. - -The following example shows you how to create a new server in the data center created above: - -```go -req := profitbricks.Server{ - Properties: profitbricks.ServerProperties{ - Name: "go01", - Ram: 1024, - Cores: 2, - }, -} -server := CreateServer(datacenter.Id, req) -``` - -## How To: List Available Images - -A list of disk and ISO images are available from ProfitBricks for immediate use. These can be easily viewed and selected. The following shows you how to get a list of images. This list represents both CDROM images and HDD images. - -```go -images := profitbricks.ListImages() -``` - -This will return a [collection](#Collection) object - -## How To: Create Storage Volume - -ProfitBricks allows for the creation of multiple storage volumes that can be attached and detached as needed. It is useful to attach an image when creating a storage volume. The storage size is in gigabytes. - -```go -volumerequest := profitbricks.Volume{ - Properties: profitbricks.VolumeProperties{ - Size: 1, - Name: "Volume Test", - LicenceType: "LINUX", - Type: "HDD", - }, -} - -storage := CreateVolume(datacenter.Id, volumerequest) -``` - -## How To: Update Cores and Memory - -ProfitBricks allows users to dynamically update cores, memory, and disk independently of each other. This removes the restriction of needing to upgrade to the next size available size to receive an increase in memory. You can now simply increase the instances memory keeping your costs in-line with your resource needs. - -Note: The memory parameter value must be a multiple of 256, e.g. 256, 512, 768, 1024, and so forth. - -The following code illustrates how you can update cores and memory: - -```go -serverupdaterequest := profitbricks.ServerProperties{ - Cores: 1, - Ram: 256, -} - -resp := PatchServer(datacenter.Id, server.Id, serverupdaterequest) -``` - -## How To: Attach or Detach Storage Volume - -ProfitBricks allows for the creation of multiple storage volumes. You can detach and reattach these on the fly. This allows for various scenarios such as re-attaching a failed OS disk to another server for possible recovery or moving a volume to another location and spinning it up. - -The following illustrates how you would attach and detach a volume and CDROM to/from a server: - -```go -profitbricks.AttachVolume(datacenter.Id, server.Id, volume.Id) -profitbricks.AttachCdrom(datacenter.Id, server.Id, images.Items[0].Id) - -profitbricks.DetachVolume(datacenter.Id, server.Id, volume.Id) -profitbricks.DetachCdrom(datacenter.Id, server.Id, images.Items[0].Id) -``` - -## How To: List Servers, Volumes, and Data Centers - -Go SDK provides standard functions for retrieving a list of volumes, servers, and datacenters. - -The following code illustrates how to pull these three list types: - -```go -volumes := profitbricks.ListVolumes(datacenter.Id) - -servers := profitbricks.ListServers(datacenter.Id) - -datacenters := profitbricks.ListDatacenters() -``` - -## Example - -```go -package main - -import ( - "fmt" - "time" - - "github.com/profitbricks/profitbricks-sdk-go" -) - -func main() { - - //Sets username and password - profitbricks.SetAuth("username", "password") - //Sets depth. - profitbricks.SetDepth("5") - - dcrequest := profitbricks.Datacenter{ - Properties: profitbricks.DatacenterProperties{ - Name: "example.go3", - Description: "description", - Location: "us/lasdev", - }, - } - - datacenter := profitbricks.CreateDatacenter(dcrequest) - - serverrequest := profitbricks.Server{ - Properties: profitbricks.ServerProperties{ - Name: "go01", - Ram: 1024, - Cores: 2, - }, - } - server := profitbricks.CreateServer(datacenter.Id, serverrequest) - - volumerequest := profitbricks.Volume{ - Properties: profitbricks.VolumeProperties{ - Size: 1, - Name: "Volume Test", - LicenceType: "LINUX", - Type: "HDD", - }, - } - - storage := profitbricks.CreateVolume(datacenter.Id, volumerequest) - - serverupdaterequest := profitbricks.ServerProperties{ - Name: "go01renamed", - Cores: 1, - Ram: 256, - } - - profitbricks.PatchServer(datacenter.Id, server.Id, serverupdaterequest) - //It takes a moment for a volume to be provisioned so we wait. - time.Sleep(60 * time.Second) - - profitbricks.AttachVolume(datacenter.Id, server.Id, storage.Id) - - volumes := profitbricks.ListVolumes(datacenter.Id) - fmt.Println(volumes.Items) - servers := profitbricks.ListServers(datacenter.Id) - fmt.Println(servers.Items) - datacenters := profitbricks.ListDatacenters() - fmt.Println(datacenters.Items) - - profitbricks.DeleteServer(datacenter.Id, server.Id) - profitbricks.DeleteDatacenter(datacenter.Id) -} -``` - -# Support -You are welcome to contact us with questions or comments at [ProfitBricks DevOps Central](https://devops.profitbricks.com/). Please report any issues via [GitHub's issue tracker](https://github.com/profitbricks/profitbricks-sdk-go/issues). \ No newline at end of file +# Go SDK + +Version: profitbricks-sdk-go **4.0.2** + +The ProfitBricks Client Library for [Go](https://www.golang.org/) provides you with access to the ProfitBricks REST API. It is designed for developers who are building applications in Go. + +This guide will walk you through getting setup with the library and performing various actions against the API. + +## Table of Contents + +* [Description](#description) +* [Getting Started](#getting-started) + * [Installation](#installation) + * [Authenticating](#authenticating) + * [Error Handling](#error-handling) +* [Reference](#reference) + * [Data Centers](#data-centers) + * [List Data Centers](#list-data-centers) + * [Retrieve a Data Center](#retrieve-a-data-center) + * [Create a Data Center](#create-a-data-center) + * [Update a Data Center](#update-a-data-center) + * [Delete a Data Center](#delete-a-data-center) + * [Locations](#locations) + * [List Locations](#list-locations) + * [Get a Location](#get-a-location) + * [Servers](#servers) + * [List Servers](#list-servers) + * [Retrieve a Server](#retrieve-a-server) + * [Create a Server](#create-a-server) + * [Update a Server](#update-a-server) + * [Delete a Server](#delete-a-server) + * [List Attached Volumes](#list-attached-volumes) + * [Attach a Volume](#attach-a-volume) + * [Retrieve an Attached Volume](#retrieve-an-attached-volume) + * [Detach a Volume](#detach-a-volume) + * [List Attached CD-ROMs](#list-attached-cd-roms) + * [Attach a CD-ROM](#attach-a-cd-rom) + * [Retrieve an Attached CD-ROM](#retrieve-an-attached-cd-rom) + * [Detach a CD-ROM](#detach-a-cd-rom) + * [Reboot a Server](#reboot-a-server) + * [Start a Server](#start-a-server) + * [Stop a Server](#stop-a-server) + * [Images](#images) + * [List Images](#list-images) + * [Get an Image](#get-an-image) + * [Update an Image](#update-an-image) + * [Delete an Image](#delete-an-image) + * [Volumes](#volumes) + * [List Volumes](#list-volumes) + * [Get a Volume](#get-a-volume) + * [Create a Volume](#create-a-volume) + * [Update a Volume](#update-a-volume) + * [Delete a Volume](#delete-a-volume) + * [Create a Volume Snapshot](#create-a-volume-snapshot) + * [Restore a Volume Snapshot](#restore-a-volume-snapshot) + * [Snapshots](#snapshots) + * [List Snapshots](#list-snapshots) + * [Get a Snapshot](#get-a-snapshot) + * [Update a Snapshot](#update-a-snapshot) + * [Delete a Snapshot](#delete-a-snapshot) + * [IP Blocks](#ip-blocks) + * [List IP Blocks](#list-ip-blocks) + * [Get an IP Block](#get-an-ip-block) + * [Create an IP Block](#create-an-ip-block) + * [Delete an IP Block](#delete-an-ip-block) + * [LANs](#lans) + * [List LANs](#list-lans) + * [Create a LAN](#create-a-lan) + * [Get a LAN](#get-a-lan) + * [Update a LAN](#update-a-lan) + * [Delete a LAN](#delete-a-lan) + * [Network Interfaces (NICs)](#network-interfaces-nics) + * [List NICs](#list-nics) + * [Get a NIC](#get-a-nic) + * [Create a NIC](#create-a-nic) + * [Update a NIC](#update-a-nic) + * [Delete a NIC](#delete-a-nic) + * [Firewall Rules](#firewall-rules) + * [List Firewall Rules](#list-firewall-rules) + * [Get a Firewall Rule](#get-a-firewall-rule) + * [Create a Firewall Rule](#create-a-firewall-rule) + * [Update a Firewall Rule](#update-a-firewall-rule) + * [Delete a Firewall Rule](#delete-a-firewall-rule) + * [Load Balancers](#load-balancers) + * [List Load Balancers](#list-load-balancers) + * [Get a Load Balancer](#get-a-load-balancer) + * [Create a Load Balancer](#create-a-load-balancer) + * [Update a Load Balancer](#update-a-load-balancer) + * [List Load Balanced NICs](#list-load-balanced-nics) + * [Get a Load Balanced NIC](#get-a-load-balanced-nic) + * [Associate NIC to a Load Balancer](#associate-nic-to-a-load-balancer) + * [Remove a NIC Association](#remove-a-nic-association) + * [Requests](#requests) + * [List Requests](#list-requests) + * [Get a Request](#get-a-request) + * [Get a Request Status](#get-a-request-status) + * [Contract Resources](#contract-resources) + * [Users Management](#users-management) + * [List Groups](#list-groups) + * [Retrieve a Group](#retrieve-a-group) + * [Create a Group](#create-a-group) + * [Update a Group](#update-a-group) + * [Delete a Group](#delete-a-group) + * [List Shares](#list-shares) + * [Retrieve a Share](#retrieve-a-share) + * [Add a Share](#add-a-share) + * [Update a Share](#update-a-share) + * [Delete a Share](#delete-a-share) + * [List Users in a Group](#list-users-in-a-group) + * [Add User to Group](#add-user-to-group) + * [Remove User from a Group](#remove-user-from-a-group) + * [List Users](#list-users) + * [Retrieve a User](#retrieve-a-user) + * [Create a User](#create-a-user) + * [Update a User](#update-a-user) + * [Delete a User](#delete-a-user) + * [List Resources](#list-resources) + * [List All Resources of a Type](#list-all-resources-of-a-type) + * [List a specific Resource Type](#list-a-specific-resource-type) +* [Example](#example) +* [Support](#support) +* [Testing](#testing) +* [Contributing](#contributing) + + +# Description + +The Go SDK wraps the latest version of the ProfitBricks REST API. All API operations are performed over SSL and authenticated using your ProfitBricks portal credentials. The API can be accessed within an instance running in ProfitBricks or directly over the Internet from any application that can send an HTTPS request and receive an HTTPS response. + +## Getting Started + +Before you begin you will need to have [signed-up](https://www.profitbricks.com/signup) for a ProfitBricks account. The credentials you setup during sign-up will be used to authenticate against the API. + +### Installation + +Install the Go language from: [Go Installation](https://golang.org/doc/install) + +The `GOPATH` environment variable specifies the location of your Go workspace. It is likely the only environment variable you'll need to set when developing Go code. This is an example of pointing to a workspace configured underneath your home directory: + +``` +mkdir -p ~/go/bin +export GOPATH=~/go +export GOBIN=$GOPATH/bin +export PATH=$PATH:$GOBIN +``` + + +The following go command will download `profitbricks-sdk-go` to your configured `GOPATH`: + +```go +go get "github.com/profitbricks/profitbricks-sdk-go" +``` + +The source code of the package will be located at: + + $GOBIN\src\profitbricks-sdk-go + +Create main package file *example.go*: + +```go +package main + +import ( + "fmt" +) + +func main() { +} +``` + +Import GO SDK: + +```go +import( + "github.com/profitbricks/profitbricks-sdk-go" +) +``` + + +### Authenticating +Add your credentials for connecting to ProfitBricks: + +```go +profitbricks.SetAuth("username", "password") +``` + + + +**Caution**: You will want to ensure you follow security best practices when using credentials within your code or stored in a file. + +### Error Handling + +The SDK will raise custom exceptions when the Cloud API returns an error. There are four response types: + +| HTTP Code | Description | +|---|---| +| 401 | The supplied user credentials are invalid. | +| 404 | The requested resource cannot be found. | +| 422 | The request body includes invalid JSON. | +| 429 | The Cloud API rate limit has been exceeded. | + +# Concepts + +The Go SDK wraps the latest version of the ProfitBricks REST API. All API operations are performed over SSL and authenticated using your ProfitBricks portal credentials. The API can be accessed within an instance running in ProfitBricks or directly over the Internet from any application that can send an HTTPS request and receive an HTTPS response. + +# Getting Started + +Before you begin you will need to have [signed-up](https://www.profitbricks.com/signup) for a ProfitBricks account. The credentials you setup during sign-up will be used to authenticate against the API. + +Install the Go language from: [Go Installation](https://golang.org/doc/install) + +The `GOPATH` environment variable specifies the location of your Go workspace. It is likely the only environment variable you'll need to set when developing Go code. This is an example of pointing to a workspace configured underneath your home directory: + +``` +mkdir -p ~/go/bin +export GOPATH=~/go +export GOBIN=$GOPATH/bin +export PATH=$PATH:$GOBIN +``` + +# Installation + +The following go command will download `profitbricks-sdk-go` to your configured `GOPATH`: + +```go +go get "github.com/profitbricks/profitbricks-sdk-go" +``` + +The source code of the package will be located at: + + $GOBIN\src\profitbricks-sdk-go + +Create main package file *example.go*: + +```go +package main + +import ( + "fmt" +) + +func main() { +} +``` + +Import GO SDK: + +```go +import( + "github.com/profitbricks/profitbricks-sdk-go" +) +``` + +Add your credentials for connecting to ProfitBricks: + +```go +profitbricks.SetAuth("username", "password") +``` + +Set depth: + +```go +profitbricks.SetDepth("5") +``` + +Depth controls the amount of data returned from the REST server ( range 1-5 ). The larger the number the more information is returned from the server. This is especially useful if you are looking for the information in the nested objects. + +**Caution**: You will want to ensure you follow security best practices when using credentials within your code or stored in a file. + + +## Reference + +This section provides details on all the available operations and the arguments they accept. Brief code snippets demonstrating usage are also included. + + +##### Depth + +Many of the *List* or *Get* operations will accept an optional *depth* argument. Setting this to a value between 0 and 5 affects the amount of data that is returned. The detail returned varies somewhat depending on the resource being queried, however it generally follows this pattern. + +| Depth | Description | +|:-:|---| +| 0 | Only direct properties are included. Children are not included. | +| 1 | Direct properties and children's references are returned. | +| 2 | Direct properties and children's properties are returned. | +| 3 | Direct properties, children's properties, and descendant's references are returned. | +| 4 | Direct properties, children's properties, and descendant's properties are returned. | +| 5 | Returns all available properties. | + +This SDK sets the *Depth=5* by default as that works well in the majority of cases. You may find that setting *Depth* to a lower or higher value could simplify a later operation by reducing or increasing the data available in the response object. + +### Data Centers + +Virtual Data Centers (VDCs) are the foundation of the ProfitBricks platform. VDCs act as logical containers for all other objects you will be creating, e.g., servers. You can provision as many VDCs as you want. VDCs have their own private network and are logically segmented from each other to create isolation. + +#### List Data Centers + +This operation will list all currently provisioned VDCs that your account credentials provide access to. + +There are no request arguments that need to be supplied. + +Call `ListDatacenters`: + + ListDatacenters() + +--- + +#### Retrieve a Data Center + +Use this to retrieve details about a specific VDC. + +The following table describes the request arguments: + +| Name | Required | Type | Description | +|---|---|---|---| +| dcid | Yes | string | The ID of the data center. | + +Pass the arguments to `GetDatacenter`: + + GetDatacenter(dcid string) + +--- + +#### Create a Data Center + +Use this operation to create a new VDC. You can create a "simple" VDC by supplying just the required *Name* and *Location* arguments. This operation also has the capability of provisioning a "complex" VDC by supplying additional arguments for servers, volumes, LANs, and/or load balancers. + +The following table describes the request arguments: + +| Name | Required | Type | Description | +|---|:-:|---|---| +| datacenter | **yes** | object | A [Datacenter object](#datacenter-resource-object) describing the VDC being created. | + +Build the `Datacenter` resource object: + + var obj = Datacenter{ + Properties: DatacenterProperties{ + Name: "GO SDK Test", + Description: "GO SDK test datacenter", + Location: location, + }, + } + +Pass the object to `CreateDatacenter`: + + CreateDatacenter(obj) + +##### Datacenter Resource Object + +| Name | Required | Type | Description | +|---|:-:|---|---| +| Name | **yes** | string | The name of the VDC. | +| Location | **yes** | string | The physical ProfitBricks location where the VDC will be created. | +| Description | no | string | A description for the VDC, e.g. staging, production. | +| Servers | no | list | A list of one or more [Server objects](#server-resource-object) to be created. | +| Volumes | no | list | A list of one or more [Volume objects](#volume-resource-object) to be created. | +| Lans | no | list | A list of one or more [LAN objects](#lan-resource-object) to be created. | +| Loadbalancers | no | list | A list of one or more [LoadBalancer objects](#load-balancer-resource-object) to be created. | + +The following table outlines the locations currently supported: + +| Value| Country | City | +|---|---|---| +| us/las | United States | Las Vegas | +| us/ewr | United States | Newark | +| de/fra | Germany | Frankfurt | +| de/fkb | Germany | Karlsruhe | + +**NOTES**: + +* The value for `Name` cannot contain the following characters: (@, /, , |, ‘’, ‘). +* You cannot change the VDC `Location` once it has been provisioned. + +--- + +#### Update a Data Center + +After retrieving a VDC, either by ID or as a create response object, you can change its properties by calling the `update_datacenter` method. Some arguments may not be changed using `update_datacenter`. + +The following table describes the available request arguments: + +| Name | Required | Type | Description | +|---|:-:|---|---| +| dcid | **yes** | string | The ID of the VDC. | +| Name | no | string | The new name of the VDC. | +| Description | no | string | The new description of the VDC. | + +Build the `DatacenterProperties` resource object: + + var obj = DatacenterProperties{Name: "new Name",Description: "new desc"} + +Pass the arguments to `PatchDatacenter`: + +PatchDatacenter(dcid string, obj DatacenterProperties) + +--- + +#### Delete a Data Center + +This will remove all objects within the VDC and remove the VDC object itself. + +**NOTE**: This is a highly destructive operation which should be used with extreme caution! + +The following table describes the available request arguments: + +| Name | Required | Type | Description | +|---|:-:|---|---| +| dcid | **yes** | string | The ID of the VDC that you want to delete. | + +Pass the argument to `DeleteDatacenter`: + + DeleteDatacenter(dcid) + +--- + +### Locations + +Locations are the physical ProfitBricks data centers where you can provision your VDCs. + +#### List Locations + +The `ListLocations` operation will return the list of currently available locations. + +There are no request arguments to supply. + + ListLocations() + +--- + +#### Get a Location + +Retrieves the attributes of a specific location. + +The following table describes the request arguments: + +| Name | Required | Type | Description | +|---|:-:|---|---| +| locationid | **yes** | string | The ID consisting of country/city. | + +Pass the argument to `GetLocation`: + + GetLocation("us/las") + +--- + +#### Get a Regional Location + +Retrieves the locations available in a specific region. + +The following table describes the request arguments: + +| Name | Required | Type | Description | +|---|:-:|---|---| +| regionid | **yes** | string | The ID consisting of country/city. | + +Pass the argument to `GetRegionalLocations`: + + GetRegionalLocations("us") + +--- + +### Servers + +#### List Servers + +You can retrieve a list of all the servers provisioned inside a specific VDC. + +The following table describes the request arguments: + +| Name | Required | Type | Description | +|---|:-:|---|---| +| dcid | **yes** | string | The ID of the VDC. | + +Pass the arguments to `ListServers`: + + ListServers(dcid) + +--- + +#### Retrieve a Server + +Returns information about a specific server such as its configuration, provisioning status, etc. + +The following table describes the request arguments: + +| Name | Required | Type | Description | +|---|:-:|---|---| +| dcId | **yes** | string | The ID of the VDC. | +| serverId | **yes** | string | The ID of the server. | + +Pass the arguments to `GetServer`: + + GetServer(dcId, serverId) + +--- + +#### Create a Server + +Creates a server within an existing VDC. You can configure additional properties such as specifying a boot volume and connecting the server to a LAN. + +The following table describes the request arguments: + +| Name | Required | Type | Description | +|---|:-:|---|---| +| datacenterId | **yes** | string | The ID of the VDC. | +| server | **yes** | object | A [Server object](#server-resource-object) describing the server being created. | + +Build a [Server](#server-resource-object) object: + + var server = Server{ + Properties: ServerProperties{ + Name: "GO SDK Test", + Ram: 1024, + Cores: 1, + AvailabilityZone: "ZONE_1", + CpuFamily: "INTEL_XEON", + }, + } + +Pass the object and other arguments to `CreateServer`: + + CreateServer(datacenterId, server) + +##### Server Resource Object + +| Name | Required | Type | Description | +|---|:-:|---|---| +| Name | **yes** | string | The name of the server. | +| Cores | **yes** | int | The total number of cores for the server. | +| Ram | **yes** | int | The amount of memory for the server in MB, e.g. 2048. Size must be specified in multiples of 256 MB with a minimum of 256 MB; however, if you set `RamHotPlug` to *true* then you must use a minimum of 1024 MB. | +| AvailabilityZone | no | string | The availability zone in which the server should exist. | +| CpuFamily | no | string | Sets the CPU type. "AMD_OPTERON" or "INTEL_XEON". Defaults to "AMD_OPTERON". | +| BootVolume | no | string | A volume ID that the server will boot from. If not *nil* then `BootCdrom` has to be *nil*. | +| BootCdrom | no | string | A CD-ROM image ID used for booting. If not *nil* then `BootVolume` has to be *nil*. | +| Cdroms | no | list | A list of existing volume IDs that you want to connect to the server. | +| Volumes | no | list | One or more [Volume objects](#volume-resource-object) that you want to create and attach to the server.| +| Nics | no | list | One or more [NIC objects](#nic-resource-object) that you wish to create at the time the server is provisioned. | + +The following table outlines the server availability zones currently supported: + +| Availability Zone | Comment | +|---|---| +| AUTO | Automatically Selected Zone | +| ZONE_1 | Fire Zone 1 | +| ZONE_2 | Fire Zone 2 | + +--- + +#### Update a Server + +Perform updates to the attributes of a server. + +The following table describes the request arguments: + +| Name | Required | Type | Description | +|---|:-:|---|---| +| datacenterId | **yes** | string | The ID of the VDC. | +| serverId | **yes** | string | The ID of the server. | +| Name | no | string | The name of the server. | +| Cores | no | int | The number of cores for the server. | +| Ram | no | int | The amount of memory in the server. | +| AvailabilityZone | no | string | The new availability zone for the server. | +| CpuFamily | no | string | Sets the CPU type. "AMD_OPTERON" or "INTEL_XEON". Defaults to "AMD_OPTERON". | +| BootVolume | no | string | A volume ID used for booting. If not *nil* then `BootCdrom` has to be *nil*. | +| BootCdrom | no | string | A CD-ROM image ID used for booting. If not *nil* then `BootVolume` has to be *nil*. | + +Build a [ServerProperties](#serverproperties) object: + + var server = ServerProperties{ + Name: "GO SDK Test RENAME", + } + + +Pass the arguments to `update_server`: + + PatchServer(datacenterId, serverId, server) + +--- + +#### Delete a Server + +This will remove a server from a VDC. **NOTE**: This will not automatically remove the storage volume(s) attached to a server. A separate operation is required to delete a storage volume. + +The following table describes the request arguments: + +| Name | Required | Type | Description | +|---|:-:|---|---| +| datacenterId | **yes** | string | The ID of the VDC. | +| serverId | **yes** | string | The ID of the server that will be deleted. | + +Pass the arguments to `delete_server`: + + DeleteServer(datacenterId, serverId) + +--- + +#### List Attached Volumes + +Retrieves a list of volumes attached to the server. + +The following table describes the request arguments: + +| Name | Required | Type | Description | +|---|:-:|---|---| +| datacenterId | **yes** | string | The ID of the VDC. | +| serverId | **yes** | string | The ID of the server. | + +Pass the arguments to `ListAttachedVolumes`: + + ListAttachedVolumes(datacenterId, serverId) + +--- + +#### Attach a Volume + +This will attach a pre-existing storage volume to the server. + +The following table describes the request arguments: + +| Name | Required | Type | Description | +|---|:-:|---|---| +| datacenterId | **yes** | string | The ID of the VDC. | +| serverId | **yes** | string | The ID of the server. | +| volumeId | **yes** | string | The ID of a storage volume. | + +Pass the arguments to `AttachVolume`: + +AttachVolume(datacenterId, serverId, volumeId) + +--- + +#### Retrieve an Attached Volume + +This will retrieve the properties of an attached volume. + +The following table describes the request arguments: + +| Name | Required | Type | Description | +|---|:-:|---|---| +| datacenterId | **yes** | string | The ID of the VDC. | +| serverId | **yes** | string | The ID of the server. | +| volumeId | **yes** | string | The ID of the attached volume. | + +Pass the arguments to `get_attached_volume`: + + GetAttachedVolume(srv_dc_id, srv_srvid, srv_vol) + +--- + +#### Detach a Volume + +This will detach the volume from the server. Depending on the volume `hot_unplug` settings, this may result in the server being rebooted. If `disc_virtio_hot_unplug` has been set to *true*, then a reboot should not be required. + +This will **NOT** delete the volume from your VDC. You will need to make a separate request to delete a volume. + +The following table describes the request arguments: + +| Name | Required | Type | Description | +|---|:-:|---|---| +| datacenterId | **yes** | string | The ID of the VDC. | +| serverId | **yes** | string | The ID of the server. | +| volumeId | **yes** | string | The ID of the attached volume. | + +Pass the arguments to `detach_volume`: + + DetachVolume(datacenterId, serverId, volumeId) + +--- + +#### List Attached CD-ROMs + +Retrieves a list of CD-ROMs attached to a server. + +The following table describes the request arguments: + +| Name | Required | Type | Description | +|---|:-:|---|---| +| datacenterId | **yes** | string | The ID of the VDC. | +| serverId | **yes** | string | The ID of the server. | + +Pass the arguments to `ListAttachedCdroms`: + + ListAttachedCdroms(srv_dc_id, srv_srvid) + +--- + +#### Attach a CD-ROM + +You can attach a CD-ROM to an existing server. + +The following table describes the request arguments: + +| Name | Required | Type | Description | +|---|:-:|---|---| +| datacenterId | **yes** | string | The ID of the VDC. | +| serverId | **yes** | string | The ID of the server. | +| cdromId | **yes** | string | The ID of a CD-ROM. | + +Pass the arguments to `attach_cdrom`: + + AttachCdrom(datacenterId, serverId, cdromId) + +--- + +#### Retrieve an Attached CD-ROM + +You can retrieve a specific CD-ROM attached to the server. + +The following table describes the request arguments: + +| Name | Required | Type | Description | +|---|:-:|---|---| +| datacenterId | **yes** | string | The ID of the VDC. | +| serverId | **yes** | string | The ID of the server. | +| cdromId | **yes** | string | The ID of the attached CD-ROM. | + +Pass the arguments to `GetAttachedCdrom`: + +GetAttachedCdrom(datacenterId, serverId, cdromId) + +--- + +#### Detach a CD-ROM + +This will detach a CD-ROM from the server. + +The following table describes the request arguments: + +| Name | Required | Type | Description | +|---|:-:|---|---| +| datacenterId | **yes** | string | The ID of the VDC. | +| serverId | **yes** | string | The ID of the server. | +| cdromId | **yes** | string | The ID of the attached CD-ROM. | + +Pass the arguments to `DetachCdrom`: + + DetachCdrom(datacenterId, serverId, cdromId) + +--- + +#### Reboot a Server + +This will force a hard reboot of the server. Do not use this method if you want to gracefully reboot the machine. This is the equivalent of powering off the machine and turning it back on. + +The following table describes the request arguments: + +| Name | Required | Type | Description | +|---|:-:|---|---| +| datacenterId | **yes** | string | The ID of the VDC. | +| serverId | **yes** | string | The ID of the server. | + +Pass the arguments to `RebootServer`: + + RebootServer(datacenterId, serverId) + +--- + +#### Start a Server + +This will start a server. If a DHCP assigned public IP was deallocated when the server was stopped, then a new IP will be assigned. + +The following table describes the request arguments: + +| Name | Required | Type | Description | +|---|:-:|---|---| +| datacenterId | **yes** | string | The ID of the VDC. | +| serverId | **yes** | string | The ID of the server. | + +Pass the arguments to `StartServer`: + + StartServer(datacenterId, serverId) + +--- + +#### Stop a Server + +This will stop a server. The machine will be forcefully powered off, billing will cease, and the public IP, if one is allocated, will be deallocated. + +The following table describes the request arguments: + +| Name | Required | Type | Description | +|---|:-:|---|---| +| datacenterId | **yes** | string | The ID of the VDC. | +| serverId | **yes** | string | The ID of the server. | + +Pass the arguments to `StopServer`: + + StopServer(datacenterId, serverId) + +--- + +### Images + +#### List Images + +Retrieve a list of images. + +Just call the `ListImages`: + + ListImages() + +--- + +#### Get an Image + +Retrieves the attributes of a specific image. + +The following table describes the request arguments: + +| Name | Required | Type | Description | +|---|:-:|---|---| +| imgId | **yes** | string | The ID of the image. | + +Pass the arguments to `GetImage`: + + GetImage(imgid) + +--- + + +### Volumes + +#### List Volumes + +Retrieve a list of volumes within the VDC. If you want to retrieve a list of volumes attached to a server please see the [List Attached Volumes](#list-attached-volumes) entry in the Server section for details. + +The following table describes the request arguments: + +| Name | Required | Type | Description | +|---|:-:|---|---| +| datacenterId | **yes** | string | The ID of the VDC. | + +Pass the arguments to `ListVolumes`: + + ListVolumes(datacenterId) + +--- + +#### Get a Volume + +Retrieves the attributes of a given volume. + +The following table describes the request arguments: + +| Name | Required | Type | Description | +|---|:-:|---|---| +| datacenterId | **yes** | string | The ID of the VDC. | +| volumeId | **yes** | string | The ID of the volume. | + +Pass the arguments to `GetVolume`: + + GetVolume(datacenterId, volumeId) + +--- + +#### Create a Volume + +Creates a volume within the VDC. This will NOT attach the volume to a server. Please see the [Attach a Volume](#attach-a-volume) entry in the Server section for details on how to attach storage volumes. + +The following table describes the request arguments: + +| Name | Required | Type | Description | +|---|:-:|---|---| +| datacenter_id | **yes** | string | The ID of the VDC. | +| volume | **yes** | object | A [Volume object](#volume-resource-object) you wish to create. | + +Build the `Volume` resource object: + + var request = Volume{ + Properties: VolumeProperties{ + Size: 2, + Name: "GO SDK Test", + ImageAlias: "ubuntu:latest", + Bus: "VIRTIO", + SshKeys: []string{"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCoLVLHON4BSK3D8L4H79aFo..."}, + Type: "HDD", + ImagePassword: "test1234", + AvailabilityZone: "ZONE_3", + }, + } + +Pass the object and arguments to `CreateVolume`: + + CreateVolume(dcID, request) + +##### Volume Resource Object + +| Name | Required | Type | Description | +|---|:-:|---|---| +| Name | no | string | The name of the volume. | +| Size | **yes** | int | The size of the volume in GB. | +| Bus | no | string | The bus type of the volume (VIRTIO or IDE). Default: VIRTIO. | +| Image | **yes** | string | The image or snapshot ID. Can be left empty for a data volume, however you'll need to set the `licence_type`. Default: *null* | +| Type | **yes** | string | The volume type, HDD or SSD. Default: HDD| +| LicenceType | **yes** | string | The licence type of the volume. Options: LINUX, WINDOWS, WINDOWS2016, UNKNOWN, OTHER. Default: UNKNOWN | +| ImagePassword | **yes** | string | A password to set on the volume for the appropriate root or administrative account. This field may only be set in creation requests. When reading, it always returns *null*. The password has to contain 8-50 characters. Only these characters are allowed: [abcdefghjkmnpqrstuvxABCDEFGHJKLMNPQRSTUVX23456789] | +| ImageAlias | **yes** | string | An alias to a ProfitBricks public image. Use instead of "image".] | +| SshKeys | **yes** | string | SSH keys to allow access to the volume via SSH. | +| AvailabilityZone | no | string | The storage availability zone assigned to the volume. Valid values: AUTO, ZONE_1, ZONE_2, or ZONE_3. This only applies to HDD volumes. Leave blank or set to AUTO when provisioning SSD volumes. | + +The following table outlines the various licence types you can define: + +| Licence Type | Comment | +|---|---| +| WINDOWS2016 | Use this for the Microsoft Windows Server 2016 operating system. | +| WINDOWS | Use this for the Microsoft Windows Server 2008 and 2012 operating systems. | +| LINUX |Use this for Linux distributions such as CentOS, Ubuntu, Debian, etc. | +| OTHER | Use this for any volumes that do not match one of the other licence types. | +| UNKNOWN | This value may be inherited when you've uploaded an image and haven't set the license type. Use one of the options above instead. | + +The following table outlines the storage availability zones currently supported: + +| Availability Zone | Comment | +|---|---| +| AUTO | Automatically Selected Zone | +| ZONE_1 | Fire Zone 1 | +| ZONE_2 | Fire Zone 2 | +| ZONE_3 | Fire Zone 3 | + +**Note:** You will need to provide either the `Image` or the `LicenceType` arguments when creating a volume. A `LicenceType` is required, but if `Image` is supplied, it is already set and cannot be changed. Either the `ImagePassword` or `SshKeys` arguments need to be supplied when creating a volume using one of the official ProfitBricks images. Only official ProfitBricks provided images support the `SshKeys` and `ImagePassword` arguments. + +--- + +#### Update a Volume + +You can update various attributes of an existing volume; however, some restrictions are in place: + +You can increase the size of an existing storage volume. You cannot reduce the size of an existing storage volume. The volume size will be increased without requiring a reboot if the relevant hot plug settings (`disc_virtio_hot_plug`, `disc_virtio_hot_unplug`, etc.) have been set to *true*. The additional capacity is not added automatically added to any partition, therefore you will need to handle that inside the OS afterwards. Once you have increased the volume size you cannot decrease the volume size. + +Since an existing volume is being modified, none of the request arguments are specifically required as long as the changes being made satisfy the requirements for creating a volume. + +The following table describes the request arguments: + +| Name | Required | Type | Description | +|---|:-:|---|---| +| datacenterId | **yes** | string | The ID of the VDC. | +| volumeId | **yes** | string | The ID of the volume. | +| Name | no | string | The name of the volume. | +| Size | no | int | The size of the volume in GB. You may only increase the `size` when updating. | +| Bus | no | string | The bus type of the volume (VIRTIO or IDE). Default: VIRTIO. | +| LicenceType | no | string | The licence type of the volume. Options: LINUX, WINDOWS, WINDOWS2016, UNKNOWN, OTHER. You may get an error trying to update `LicenceType` depending on the `Image` that was used to create the volume. For example, you cannot update the `LicenceType` for a volume created from a ProfitBricks supplied OS image. | + +**Note**: Trying to change the `Image`, `Type`, or `AvailabilityZone` in an update request will result in an error. + +Pass the arguments to `PatchVolume`: + + var obj := VolumeProperties{ + Name: "GO SDK Test - RENAME", + Size: 5, + } + PatchVolume(datacenterId, volumeId, obj) + +--- + +#### Delete a Volume + +Deletes the specified volume. This will result in the volume being removed from your data center. Use this with caution. + +The following table describes the request arguments: + +| Name | Required | Type | Description | +|---|:-:|---|---| +| datacenterId | **yes** | string | The ID of the VDC. | +| volumeId | **yes** | string | The ID of the volume. | + +Pass the arguments to `DeleteVolume`: + + DeleteVolume(datacenterId, volumeId) + +--- + +#### Create a Volume Snapshot + +Creates a snapshot of a volume within the VDC. You can use a snapshot to create a new storage volume or to restore a storage volume. + +The following table describes the request arguments: + +| Name | Required | Type | Description | +|---|:-:|---|---| +| datacenterId | **yes** | string | The ID of the VDC. | +| volumeId | **yes** | string | The ID of the volume. | +| Name | no | string | The name of the snapshot. | +| Description | no | string | The description of the snapshot. | + +Pass the arguments to `CreateSnapshot`: + + CreateSnapshot(datacenterId, volumeId, Name,Description) + +--- + +#### Restore a Volume Snapshot + +This will restore a snapshot onto a volume. A snapshot is created as just another image that can be used to create new volumes or to restore an existing volume. + +The following table describes the request arguments: + +| Name | Required | Type | Description | +|---|:-:|---|---| +| datacenterId | **yes** | string | The ID of the VDC. | +| volumeId | **yes** | string | The ID of the volume. | +| snapshotId | **yes** | string | The ID of the snapshot. | + +Pass the arguments to `restore_snapshot`: + + RestoreSnapshot(datacenterId, volumeId, snapshotId) + +--- + +### Snapshots + +#### List Snapshots + +Call the `ListSnapshots`: + + ListSnapshots() + +--- + +#### Get a Snapshot + +Retrieves the attributes of a specific snapshot. + +The following table describes the request arguments: + +| Name | Required | Type | Description | +|---|:-:|---|---| +| snapshotId | **yes** | string | The ID of the snapshot. | + +Pass the arguments to `GetSnapshot`: + + GetSnapshot(snapshotId) + +--- + +#### Update a Snapshot + +Perform updates to attributes of a snapshot. + +The following table describes the request arguments: + +| Name | Required | Type | Description | +|---|:-:|---|---| +| snapshotId | **yes** | string | The ID of the snapshot. | +| Name | no | string | The name of the snapshot. | +| Description | no | string | The description of the snapshot. | +| LicenceType | no | string | The snapshot's licence type: LINUX, WINDOWS, WINDOWS2016, or OTHER. | +| CpuHotPlug | no | bool | This volume is capable of CPU hot plug (no reboot required) | +| CpuHotUnplug | no | bool | This volume is capable of CPU hot unplug (no reboot required) | +| RamHotPlug | no | bool | This volume is capable of memory hot plug (no reboot required) | +| RamHotUnplug | no | bool | This volume is capable of memory hot unplug (no reboot required) | +| NicHotPlug | no | bool | This volume is capable of NIC hot plug (no reboot required) | +| NicHotUnplug | no | bool | This volume is capable of NIC hot unplug (no reboot required) | +| DiscVirtioHotPlug | no | bool | This volume is capable of VirtIO drive hot plug (no reboot required) | +| DiscVirtioHotUnplug | no | bool | This volume is capable of VirtIO drive hot unplug (no reboot required) | +| DiscScsiHotPlug | no | bool | This volume is capable of SCSI drive hot plug (no reboot required) | +| DiscScsiHotUnplug | no | bool | This volume is capable of SCSI drive hot unplug (no reboot required) | + +Pass the arguments to `UpdateSnapshot`: + + UpdateSnapshot(snapshotId, SnapshotProperties{Name: newValue}) + +--- + +#### Delete a Snapshot + +Deletes the specified snapshot. + +The following table describes the request arguments: + +| Name | Required | Type | Description | +|---|:-:|---|---| +| snapshotId | **yes** | string | The ID of the snapshot. | + +Pass the arguments to `DeleteSnapshot`: + + DeleteSnapshot(snapshotId) + +--- + +### IP Blocks + +The IP block operations assist with managing reserved /static public IP addresses. + +#### List IP Blocks + +Retrieve a list of available IP blocks. + + + ListIpBlocks() + +--- + +#### Get an IP Block + +Retrieves the attributes of a specific IP block. + +The following table describes the request arguments: + +| Name | Required | Type | Description | +|---|:-:|---|---| +| ipblock_id | **yes** | string | The ID of the IP block. | + +Pass the arguments to `get_ipblock`: + + response = client.get_ipblock('UUID') + +--- + +#### Create an IP Block + +Creates an IP block. Creating an IP block is a bit different than some of the other available create operations. IP blocks are not attached to a particular VDC, but rather to a location. Therefore, you must specify a valid `location` along with a `size` argument indicating the number of IP addresses you want to reserve in the IP block. Any resources using an IP address from an IP block must be in the same `location`. + +The following table describes the request arguments: + +| Name | Required | Type | Description | +|---|:-:|---|---| +| datacenter_id | **yes** | string | The ID of the VDC. | +| ipblock | **yes** | object | An [IPBlock object](#ipblock-resource-object) you wish to create. | + +To create an IP block, define the `IPBlock` resource object: + + var ipblock = IpBlock{ + Properties: IpBlockProperties{ + Name: "GO SDK Test", + Size: 2, + Location: location, + }, + } + +Pass it to `ReserveIpBlock`: + + ReserveIpBlock(ipblock) + +##### IPBlock Resource Object + +| Name | Required | Type | Description | +|---|:-:|---|---| +| Location | **yes** | string | This must be one of the available locations: us/las, us/ewr, de/fra, de/fkb. | +| Size | **yes** | int | The size of the IP block you want. | +| Name | no | string | A descriptive name for the IP block | + +The following table outlines the locations currently supported: + +| Value| Country | City | +|---|---|---| +| us/las | United States | Las Vegas | +| us/ewr | United States | Newark | +| de/fra | Germany | Frankfurt | +| de/fkb | Germany | Karlsruhe | + +--- + +#### Delete an IP Block + +Deletes the specified IP Block. + +The following table describes the request arguments: + +| Name | Required | Type | Description | +|---|:-:|---|---| +| ipblkid | **yes** | string | The ID of the IP block. | + +Pass the arguments to `ReleaseIpBlock`: + + ReleaseIpBlock(ipblkid) + +--- + +### LANs + +#### List LANs + +Retrieve a list of LANs within the VDC. + +The following table describes the request arguments: + +| Name | Required | Type | Description | +|---|:-:|---|---| +| datacenterd | **yes** | string | The ID of the VDC. | + + +Pass the arguments to `ListLans`: + + ListLans(datacenterd) + +--- + +#### Create a LAN + +Creates a LAN within a VDC. + +The following table describes the request arguments: + +| Name | Required | Type | Description | +|---|:-:|---|---| +| datacenterId | **yes** | string | The ID of the VDC. | +| lan | **yes** | object | A [LAN object](#lan-resource-object) describing the LAN to create. | + +Create the `LAN` resource object: + + var request = CreateLanRequest{ + Properties: CreateLanProperties{ + Public: true, + Name: "GO SDK Test with failover", + }, + Entities: &LanEntities{ + Nics: lanNics, + }, + } + +Pass the object and arguments to `create_lan`: + + CreateLan(datacenterId, request) + +##### LAN Resource Object + +| Name | Required | Type | Description | +|---|:-:|---|---| +| Name | no | string | The name of your LAN. | +| Public | **Yes** | bool | Boolean indicating if the LAN faces the public Internet or not. | +| Nics | no | list | One or more NIC IDs attached to the LAN. | + +--- + +#### Get a LAN + +Retrieves the attributes of a given LAN. + +The following table describes the request arguments: + +| Name | Required | Type | Description | +|---|:-:|---|---| +| datacenterId | **yes** | string | The ID of the VDC. | +| lanId | **yes** | int | The ID of the LAN. | + +Pass the arguments to `GetLan`: + + GetLan(datacenterId, lanId) + +--- + +#### Update a LAN + +Perform updates to attributes of a LAN. + +The following table describes the request arguments: + +| Name | Required | Type | Description | +|---|:-:|---|---| +| datacenterId | **yes** | string | The ID of the VDC. | +| lanId | **yes** | int | The ID of the LAN. | +| Name | no | string | A descriptive name for the LAN. | +| Public | no | bool | Boolean indicating if the LAN faces the public Internet or not. | +| IpFailover | no | array | A list of IP fail-over dicts. | + +Pass the arguments to `update_lan`: + + var obj = LanProperties{ + Properties: LanProperties{ + Public: true, + Name: "GO SDK Test with failover", + } + PatchLan(datacenterId, lanId, obj) + +--- + +#### Delete a LAN + +Deletes the specified LAN. + +The following table describes the request arguments: + +| Name | Required | Type | Description | +|---|:-:|---|---| +| datacenterId | **yes** | string | The ID of the VDC. | +| lanId | **yes** | string | The ID of the LAN. | + +Pass the arguments to `delete_lan`: + + DeleteLan(lan_dcid, lanid) +--- + +### Network Interfaces (NICs) + +#### List NICs + +Retrieve a list of LANs within the VDC. + +The following table describes the request arguments: + +| Name | Required | Type | Description | +|---|:-:|---|---| +| datacenterId | **yes** | string | The ID of the VDC. | +| serverId | **yes** | string | The ID of the server. | + +Pass the arguments to `ListNics`: + + ListNics(datacenterId, serverId) + +--- + +#### Get a NIC + +Retrieves the attributes of a given NIC. + +The following table describes the request arguments: + +| Name | Required | Type | Description | +|---|:-:|---|---| +| datacenterId | **yes** | string | The ID of the VDC. | +| serverId | **yes** | string | The ID of the server. | +| nicId | **yes** | string | The ID of the NIC. | + +Pass the arguments to `GetNic`: + + GetNic(datacenterId, serverId, nicId) + +--- + +#### Create a NIC + +Adds a NIC to the target server. + +The following table describes the request arguments: + +| Name | Required | Type | Description | +|---|:-:|---|---| +| datacenterId | **yes** | string | The ID of the VDC. | +| serverId | **yes** | string| The ID of the server. | +| nic | **yes** | object | A [NIC object](#nic-resource-object) describing the NIC to be created. | + +Create the `NIC` resource object: + + var nic = Nic{ + Properties: &NicProperties{ + Lan: 1, + Name: "GO SDK Test", + Nat: false, + Dhcp: true, + FirewallActive: true, + Ips: []string{"10.0.0.1"}, + }, + } + +Pass the object and arguments to `create_nic`: + + CreateNic(datacenterId, serverId, nic) + +##### NIC Resource Object + +| Name | Required | Type | Description | +|---|:-:|---|---| +| Name | no | string | The name of the NIC. | +| Ips | no | list | IP addresses assigned to the NIC. | +| Dhcp | no | bool | Set to *false* if you wish to disable DHCP on the NIC. Default: *true*. | +| Lan | **yes** | int | The LAN ID the NIC will sit on. If the LAN ID does not exist it will be created. | +| Nat | no | bool | Indicates the private IP address has outbound access to the public internet. | +| FirewallActive | no | bool | Set this to *true* to enable the ProfitBricks firewall, *false* to disable. | +| Firewallrules | no | list | A list of [FirewallRule objects](#firewall-rule-resource-object) to be created with the NIC. | + +--- + +#### Update a NIC + +You can update -- in full or partially -- various attributes on the NIC; however, some restrictions are in place: + +The primary address of a NIC connected to a load balancer can only be changed by changing the IP of the load balancer. You can also add additional reserved, public IPs to the NIC. + +The user can specify and assign private IPs manually. Valid IP addresses for private networks are 10.0.0.0/8, 172.16.0.0/12 or 192.168.0.0/16. + +The following table describes the request arguments: + +| Name | Required | Type | Description | +|---|:-:|---|---| +| datacenterId | **yes** | string | The ID of the VDC. | +| serverId | **yes** | string| The ID of the server. | +| nicId | **yes** | string| The ID of the NIC. | +| Name | no | string | The name of the NIC. | +| Ips | no | list | IPs assigned to the NIC represented as a list of strings. | +| Dhcp | no | bool | Boolean value that indicates if the NIC is using DHCP or not. | +| Lan | no | int | The LAN ID the NIC sits on. | +| Nat | no | bool | Indicates the private IP address has outbound access to the public internet. | +| FirewallActive | no | bool | Set this to *true* to enable the ProfitBricks firewall, *false* to disable. | + +Pass the arguments to `update_nic`: + + var obj = NicProperties{Name: "GO SDK Test - RENAME", Lan: 1} + PatchNic(nic_dcid, nic_srvid, nicid, obj) + +--- + +#### Delete a NIC + +Deletes the specified NIC. + +The following table describes the request arguments: + +| Name | Required | Type | Description | +|---|:-:|---|---| +| datacenterId | **yes** | string | The ID of the VDC. | +| serverId | **yes** | string| The ID of the server. | +| nicId | **yes** | string| The ID of the NIC. | + +Pass the arguments to `DeleteNic`: + + DeleteNic(nic_dcid, nic_srvid, nicid) + +--- + +### Firewall Rules + +#### List Firewall Rules + +Retrieves a list of firewall rules associated with a particular NIC. + +The following table describes the request arguments: + +| Name | Required | Type | Description | +|---|:-:|---|---| +| datacenterId | **yes** | string | The ID of the VDC. | +| serverId | **yes** | string | The ID of the server. | +| nicId | **yes** | string | The ID of the NIC. | + +Pass the arguments to `ListFirewallRules`: + + ListFirewallRules(datacenterId, serverId, nicId) + +--- + +#### Get a Firewall Rule + +Retrieves the attributes of a given firewall rule. + +The following table describes the request arguments: + +| Name | Required | Type | Description | +|---|:-:|---|---| +| datacenterId | **yes** | string | The ID of the VDC. | +| serverId | **yes** | string | The ID of the server. | +| nicId | **yes** | string | The ID of the NIC. | +| firewallRuleId | **yes** | string | The ID of the firewall rule. | + +Pass the arguments to `get_firewall_rule`: + + GetFirewallRule(datacenterId, serverId, nicId, firewallRuleId) + +--- + +#### Create a Firewall Rule + +This will add a firewall rule to the NIC. + +The following table describes the request arguments: + +| Name | Required | Type | Description | +|---|:-:|---|---| +| datacenterId | **yes** | string | The ID of the VDC. | +| serverId | **yes** | string | The ID of the server. | +| nicId | **yes** | string | The ID of the NIC. | +| firewallRule | **yes** | object | A [FirewallRule object](#firewall-rule-resource-object) describing the firewall rule to be created. | + +Create the `FirewallRule` resource object: + + var firewallRule FirewallRule{ + Properties: FirewallruleProperties{ + Name: "SSH", + Protocol: "TCP", + SourceMac: "01:23:45:67:89:00", + PortRangeStart: 22, + PortRangeEnd: 22, + }, + } + +Pass the object and arguments to `create_firewall_rule`: + + CreateFirewallRule(datacenterId, serverId, nicId, firewallRule) + +##### Firewall Rule Resource Object + +| Name | Required | Type | Description | +|---|:-:|---|---| +| Name | no | string | The name of the firewall rule. | +| Protocol | **yes** | string | The protocol for the rule: TCP, UDP, ICMP, ANY. | +| SourceMac | no | string | Only traffic originating from the respective MAC address is allowed. Valid format: aa:bb:cc:dd:ee:ff. A *nil* value allows all source MAC address. | +| SourceIp | no | string | Only traffic originating from the respective IPv4 address is allowed. A *nil* value allows all source IPs. | +| TargetIp | no | string | In case the target NIC has multiple IP addresses, only traffic directed to the respective IP address of the NIC is allowed. A *nil* value allows all target IPs. | +| PortRangeStart | no | string | Defines the start range of the allowed port (from 1 to 65534) if protocol TCP or UDP is chosen. Leave `PortRangeStart` and `PortRangeEnd` value as *nil* to allow all ports. | +| PortRangeEnd | no | string | Defines the end range of the allowed port (from 1 to 65534) if the protocol TCP or UDP is chosen. Leave `PortRangeStart` and `PortRangeEnd` value as *nil* to allow all ports. | +| IcmpType | no | string | Defines the allowed type (from 0 to 254) if the protocol ICMP is chosen. A *nil* value allows all types. | +| IcmpCode | no | string | Defines the allowed code (from 0 to 254) if protocol ICMP is chosen. A *nil* value allows all codes. | + +--- + +#### Update a Firewall Rule + +Perform updates to an existing firewall rule. You will notice that some arguments, such as `protocol` cannot be updated. If the `protocol` needs to be changed, you can [delete](#delete-a-firewall-rule) the firewall rule and then [create](#create-a-firewall-rule) new one to replace it. + +The following table describes the request arguments: + +| Name | Required | Type | Description | +|---|:-:|---|---| +| datacenterId | **yes** | string | The ID of the VDC. | +| serverId | **yes** | string | The ID of the server. | +| nicId | **yes** | string | The ID of the NIC. | +| firewallRuleId | **yes** | string | The ID of the firewall rule. | +| Name | no | string | The name of the firewall rule. | +| SourceMac | no | string | Only traffic originating from the respective MAC address is allowed. Valid format: aa:bb:cc:dd:ee:ff. A *nil* value allows all source MAC address. | +| SourceIp | no | string | Only traffic originating from the respective IPv4 address is allowed. A *nil* value allows all source IPs. | +| TargetIp | no | string | In case the target NIC has multiple IP addresses, only traffic directed to the respective IP address of the NIC is allowed. A *nil* value allows all target IPs. | +| PortRangeStart | no | string | Defines the start range of the allowed port (from 1 to 65534) if protocol TCP or UDP is chosen. Leave `PortRangeStart` and `PortRangeEnd` value as *nil* to allow all ports. | +| PortRangeEnd | no | string | Defines the end range of the allowed port (from 1 to 65534) if the protocol TCP or UDP is chosen. Leave `PortRangeStart` and `PortRangeEnd` value as *nil* to allow all ports. | +| IcmpType | no | string | Defines the allowed type (from 0 to 254) if the protocol ICMP is chosen. A *nil* value allows all types. | +| IcmpCode | no | string | Defines the allowed code (from 0 to 254) if protocol ICMP is chosen. A *nil* value allows all codes. | + +Pass the arguments to `PatchFirewallRule`: + + props := FirewallruleProperties{ + Name: "SSH - RENAME", + } + PatchFirewallRule(dcID, srv_srvid, nicid, fwId, props) + +--- + +#### Delete a Firewall Rule + +Removes a firewall rule. + +| Name | Required | Type | Description | +|---|:-:|---|---| +| datacenterId | **yes** | string | The ID of the VDC. | +| serverId | **yes** | string | The ID of the server. | +| nicId | **yes** | string | The ID of the NIC. | +| firewallRuleId | **yes** | string | The ID of the firewall rule. | + +Pass the arguments to `DeleteFirewallRule`: + + DeleteFirewallRule(dcID, srv_srvid, nicid, fwId) + +--- + +### Load Balancers + +#### List Load Balancers + +Retrieve a list of load balancers within the data center. + +The following table describes the request arguments: + +| Name | Required | Type | Description | +|---|:-:|---|---| +| datacenterId | **yes** | string | The ID of the VDC. | + + +Pass the arguments to `ListLoadbalancers`: + + ListLoadbalancers(datacenterId) + +--- + +#### Get a Load Balancer + +Retrieves the attributes of a given load balancer. + +The following table describes the request arguments: + +| Name | Required | Type | Description | +|---|:-:|---|---| +| datacenterId | **yes** | string | The ID of the VDC. | +| loadbalancerId | **yes** | string | The ID of the load balancer. | + +Pass the arguments to `GetLoadbalancer`: + + GetLoadbalancer(datacenterId, loadbalancerId) + +--- + +#### Create a Load Balancer + +Creates a load balancer within the VDC. Load balancers can be used for public or private IP traffic. + +The following table describes the request arguments: + +| Name | Required | Type | Description | +|---|:-:|---|---| +| datacenterId | **yes** | string | The ID of the VDC. | +| loadbalancer | **yes** | object | A [LoadBalancer object](#load-balancer-resource-object) describing the load balancer to be created. | + +Create the `LoadBalancer` resource object: + + var loadbalancer = Loadbalancer{ + Properties: LoadbalancerProperties{ + Name: "GO SDK Test", + Ip: "10.0.0.1", + Dhcp: true, + } + } +Pass the object and arguments to `CreateLoadbalancer`: + + CreateLoadbalancer(datacenterId, loadbalancer) + +##### Load Balancer Resource Object + +| Name | Required | Type | Description | +|---|:-:|---|---| +| Name | **yes** | string | The name of the load balancer. | +| Ip | no | string | IPv4 address of the load balancer. All attached NICs will inherit this IP. | +| Dhcp | no | bool | Indicates if the load balancer will reserve an IP using DHCP. | +| Balancednics | no | list | List of NIC IDs taking part in load-balancing. All balanced NICs inherit the IP of the load balancer. | + +--- + +#### Update a Load Balancer + +Perform updates to attributes of a load balancer. + +The following table describes the request arguments: + +| Name | Required | Type | Description | +|---|:-:|---|---| +| datacenterId | **yes** | string | The ID of the VDC. | +| loadbalancerId | **yes** | string | The ID of the load balancer. | +| Name | no | string | The name of the load balancer. | +| Ip | no | string | The IP of the load balancer. | +| Dhcp | no | bool | Indicates if the load balancer will reserve an IP using DHCP. | + +Pass the arguments to `PatchLoadbalancer`: + + var obj = LoadbalancerProperties{Name: "GO SDK Test - RENAME"} + PatchLoadbalancer(datacenterId, loadbalancerId, obj) + +--- + +#### Delete a Load Balancer + +Deletes the specified load balancer. + +The following table describes the request arguments: + +| Name | Required | Type | Description | +|---|:-:|---|---| +| datacenterId | **yes** | string | The ID of the VDC. | +| loadbalancerId | **yes** | string | The ID of the load balancer. | + +Pass the arguments to `DeleteLoadbalancer`: + + DeleteLoadbalancer(datacenterId, loadbalancerId) + +--- + +#### List Load Balanced NICs + +This will retrieve a list of NICs associated with the load balancer. + +The following table describes the request arguments: + +| Name | Required | Type | Description | +|---|:-:|---|---| +| datacenterId | **yes** | string | The ID of the VDC. | +| loadbalancerId | **yes** | string | The ID of the load balancer. | + +Pass the arguments to `ListBalancedNics`: + + ListBalancedNics(datacenterId, loadbalancerId) + +--- + +#### Get a Load Balanced NIC + +Retrieves the attributes of a given load balanced NIC. + +The following table describes the request arguments: + +| Name | Required | Type | Description | +|---|:-:|---|---| +| datacenterId | **yes** | string | The ID of the VDC. | +| loadbalancerId | **yes** | string | The ID of the load balancer. | +| nicId | **yes** | string | The ID of the NIC. | + + +Pass the arguments to `GetBalancedNic`: + + GetBalancedNic(datacenterId, loadbalancerId, nicId) + +--- + +#### Associate NIC to a Load Balancer + +This will associate a NIC to a load balancer, enabling the NIC to participate in load-balancing. + +The following table describes the request arguments: + +| Name | Required | Type | Description | +|---|:-:|---|---| +| datacenterId | **yes** | string | The ID of the VDC. | +| loadbalancerId | **yes** | string | The ID of the load balancer. | +| nicId | **yes** | string | The ID of the NIC. | + +Pass the arguments to `add_loadbalanced_nics`: + + AssociateNic(datacenterId, loadbalancerId, nicId) + +--- + +#### Remove a NIC Association + +Removes the association of a NIC with a load balancer. + +The following table describes the request arguments: + +| Name | Required | Type | Description | +|---|:-:|---|---| +| datacenterId | **yes** | string | The ID of the VDC. | +| loadbalancerId | **yes** | string | The ID of the load balancer. | +| nicId | **yes** | string | The ID of the NIC you are removing from the load balancer. | + +Pass the arguments to `DeleteBalancedNic`: + + DeleteBalancedNic(datacenterId, loadbalancerId, nicId) + +--- + +### Requests + +Each call to the ProfitBricks Cloud API is assigned a request ID. These operations can be used to get information about the requests that have been submitted and their current status. + +#### List Requests + + + ListRequests() + +--- + +#### Get a Request + +Retrieves the attributes of a specific request. This operation shares the same `get_request` method used for getting request status, however the response it determined by the boolean value you pass for *status*. To get details about the request itself, you want to pass a *status* of *False*. + +The following table describes the request arguments: + +| Name | Required | Type | Description | +|---|:-:|---|---| +| request_id | **yes** | string | The ID of the request. | +| status | **yes** | bool | Set to *False* to have the request details returned. | + +Pass the arguments to `get_request`: + + response = client.get_request( + request_id='UUID', + status=False) + +--- + +#### Get a Request Status + +Retrieves the status of a request. This operation shares the same `get_request` method used for getting the details of a request, however the response it determined by the boolean value you pass for *status*. To get the request status, you want to pass a *status* of *True*. + +The following table describes the request arguments: + +| Name | Required | Type | Description | +|---|:-:|---|---| +| path | **yes** | string | The ID of the request. Retrieved from response header location | + + +Pass the arguments to `get_request`: + + GetRequestStatus(path) + +--- + +### Contract Resources + +#### List Contract Resources + +Returns information about the resource limits for a particular contract and the current resource usage. + +``` +GetContractResources() +``` + +--- + +### Users Management +These operations are designed to allow you to orchestrate users and resources via the Cloud API. Previously this functionality required use of the DCD (Data Center Designer) web application. + +#### List Groups +This retrieves a full list of all groups. + +``` +ListGroups() +``` + + +#### Retrieve a Group +The following table describes the request arguments: + +| Name | Required | Type | Description | +|---|---|---|---| +| groupId | Yes | string | The ID of the specific group to retrieve. | + +``` +GetGroup(groupid) +``` + +#### Create a Group + +The following table describes the request arguments: + +| Name | Type | Description | Required | +|---|---|---|---| +| group | Group |See [Group Object](#group-resource-object) | Yes | + +Build the `Group` resource object: + + var group = Group{ + Properties: GroupProperties{ + Name: "GO SDK Test", + CreateDataCenter: &TRUE, + CreateSnapshot: &TRUE, + ReserveIp: &TRUE, + AccessActivityLog: &TRUE, + }, + } + +Pass the object to `CreateGroup`: + +``` +CreateGroup(group Group) +``` + +##### Group Resource Object + +| Name | Required | Type | Description | +|---|:-:|---|---| +| Name | **yes** | string | A name that was given to the group. | +| CreateDataCenter | no | bool | The group has permission to create virtual data centers. | +| CreateSnapshot | no | bool | The group has permission to create snapshots. | +| ReserveIp | no | bool | The group has permission to reserve IP addresses. | +| AccessActivityLog | no | bool | The group has permission to access the activity log. | + +#### Update a Group + +Use this operation to update a group. + +The following table describes the request arguments: + +| Name | Type | Description | Required | +| --- | --- | --- | --- | +| groupId | **yes** | string | The ID of the specific group to retrieve. | +| group | Group |See [Group Object](#group-resource-object) | Yes | + +``` +UpdateGroup(groupId, group Group) +``` + +--- + +#### Delete a Group + +This will remove all objects within the data center and remove the data center object itself. +Use this operation to delete a single group. Resources that are assigned to the group are NOT deleted, but are no longer accessible to the group members unless the member is a Contract Owner, Admin, or Resource Owner. + +The following table describes the request arguments: + +| Name | Type | Description | Required | +| --- | --- | --- | --- | +| groupId | **yes** | string | The ID of the specific group to retrieve. | + +``` +DeleteGroup(groupId) +``` + +--- + +#### List Shares +Retrieves a full list of all the resources that are shared through this group and lists the permissions granted to the group members for each shared resource. + +``` +ListShares() +``` + +#### Retrieve a Share + +The following table describes the request arguments: + +| Name | Required | Type | Description | +|---|---|---|---| +| groupid | **yes** | string | The ID of the specific group to retrieve. | +| resourceId | **yes** | string | The ID of the specific resource to retrieve. | + +``` +GetShare(groupid, resourceId) +``` + +--- + +#### Add a Share + +The following table describes the request arguments: + +| Name | Required | Type | Description | +|---|---|---|---| +| groupid | **yes** | string | The ID of the specific group to add a resource too. | +| resourceId | **yes** | string | The ID of the specific resource to add. | +| share | **yes** | Share | See [Share Object](#share-resource-object) | + +Build the `Share` resource object: + + var share = Share{ + Properties: ShareProperties{ + SharePrivilege: true, + EditPrivilege: true, + }, + } + +Pass the object to `AddShare`: + +``` +AddShare(share Share, groupid, resourceId) +``` + +##### Share Resource Object + +| Name | Required | Type | Description | +|---|:-:|---|---| +| EditPrivilege | no | bool | The group has permission to edit privileges on this resource. | +| SharePrivilege | no | bool | The group has permission to share this resource. | + +--- + +#### Update a Share + +Use this to update the permissions that a group has for a specific resource share. + +The following table describes the request arguments: + +| Name | Required | Type | Description | +|---|---|---|---| +| groupid | **yes** | string | The ID of the specific group to add a resource too. | +| resourceId | **yes** | string | The ID of the specific resource to add. | +| share | **yes** | Share | See [Share Object](#share-resource-object) | + +``` +UpdateShare(groupid, resourceId, obj) +``` + + +#### Delete a Share + +This will remove all objects within the data center and remove the data center object itself. +Use this operation to delete a single group. Resources that are assigned to the group are NOT deleted, but are no longer accessible to the group members unless the member is a Contract Owner, Admin, or Resource Owner. + +The following table describes the request arguments: + +| Name | Type | Description | Required | +| --- | --- | --- | --- | +| groupid | **yes** | string | The ID of the specific group containing the resource to delete. | +| resourceId | **yes** | string | The ID of the specific resource to delete. | + +``` +DeleteShare(groupid, resourceId) +``` + +--- + +#### List Users in a Group +Retrieves a full list of all the users that are members of a particular group. + +The following table describes the request arguments: + +| Name | Type | Description | Required | +| --- | --- | --- | --- | +| groupid | **yes** | string | The ID of the specific group to retrieve a user list for. | + +``` +ListGroupUsers(groupid) +``` + +--- + + +#### Add User to Group + +The following table describes the request arguments: + +| Name | Required | Type | Description | +|---|---|---|---| +| groupid | **yes** | string | The ID of the specific group you want to add a user to. | +| userid | **yes** | string | The ID of the specific user to add to the group. | + + +``` +AddUserToGroup(groupid, userid) +``` + +--- + +#### Remove User from a Group + +Use this operation to remove a user from a group. + +The following table describes the request arguments: + +| Name | Type | Description | Required | +| --- | --- | --- | --- | +| groupid | **yes** | string | The ID of the specific group you want to remove a user from. | +| userid | **yes** | string | The ID of the specific user to remove from the group. | + +``` +DeleteUserFromGroup(groupid, userid) +``` + +--- + +#### List Users +Retrieve a list of all the users that have been created under a contract. + +``` +ListUsers() +``` + +--- + +#### Retrieve a User +Retrieve details about a specific user including what groups and resources the user is associated with. + +The following table describes the request arguments: + +| Name | Required | Type | Description | +|---|---|---|---| +| userid | **yes** | string | The ID of the specific user to retrieve information about. | + +``` +GetUser(userid) +``` + +--- + +#### Create a User +Creates a new user under a particular contract. + + +The following table describes the request arguments: + +| Name | Required | Type | Description | +|---|---|---|---| +| user | **yes** | User | See [User Object](#user-resource-object) | + +Build the `User` resource object: + + var user = User{ + Properties: &UserProperties{ + Firstname: "John", + Lastname: "Doe", + Email: email, + Password: "abc123-321CBA", + Administrator: false, + ForceSecAuth: false, + SecAuthActive: false, + }, + } + +Pass the object to `CreateUser`: + +``` +CreateUser(user User) +``` + +##### User Resource Object + +| Name | Required | Type | Description | +|---|:-:|---|---| +| Firstname | **yes** | bool | The first name of the user. | +| Lastname | **yes** | bool | The last name of the user. | +| Email | **yes** | bool | The e-mail address of the user. | +| Password | **yes** | bool | A password for the user. | +| Administrator | no | bool | Indicates if the user has administrative rights. | +| ForceSecAuth | no | bool | Indicates if secure (two-factor) authentication was enabled for the user. | +| SecAuthActive | no | bool | Indicates if secure (two-factor) authentication is enabled for the user. | + +--- + +#### Update a User + +Update details about a specific user including their privileges. + +The following table describes the request arguments: + +| Name | Required | Type | Description | +|---|---|---|---| +| userid | **Yes** | string | The ID of the specific user to update. | + + +``` +user := UserProperties{ + Firstname: "go sdk ", + Lastname: newName, + Email: "test@go.com", + Password: "abc123-321CBA", + Administrator: false, + ForceSecAuth: false, + SecAuthActive: false, + } +UpdateUser(userid, user) +``` + +--- + +#### Delete a User + +Blacklists the user, disabling them. The user is not completely purged, therefore if you anticipate needing to create a user with the same name in the future, we suggest renaming the user before you delete it. + +The following table describes the request arguments: + +| Name | Type | Description | Required | +| --- | --- | --- | --- | +| userid | **Yes** | string | The ID of the specific user to update. | + +``` +DeleteUser(userid) +``` + +--- + +#### List Resources +Retrieves a list of all resources and optionally their group associations. + +*Note*: This API call can take a significant amount of time to return when there are a large number of provisioned resources. You may wish to consult the next section on how to list resources of a particular type. + +``` +ListResources() +``` + +--- + +#### List All Resources of a Type +Lists all shareable resources of a specific type. Optionally include their association with groups, permissions that a group has for the resource, and users that are members of the group. Because you are scoping your request to a specific resource type, this API will likely return faster than querying `/um/resources`. + + + +The following table describes the request arguments: + +| Name | Type | Description | Required | +| --- | --- | --- | --- | +| resourcetype | **Yes** | string | The specific type of resources to retrieve information about. | + +The values available for resourcetype are listed in this table: + +| Resource Type | Description | +|---|---| +| datacenter | A virtual data center. | +| image | A private image that has been uploaded to ProfitBricks. | +| snapshot | A snapshot of a storage volume. | +| ipblock | An IP block that has been reserved. | + +``` +ListResourcesByType(resourcetype) +``` + +--- + +#### List a specific Resource Type + + +The following table describes the request arguments: + +| Name | Type | Description | Required | +| --- | --- | --- | --- | +| resourcetype | **Yes** | string | The specific type of resources to retrieve information about. | +| resourceId | **Yes** | string | The ID of the specific resource to retrieve information about. | + +The values available for resourcetype are listed in this table: + +| Resource Type | Description | +|---|---| +| datacenter | A virtual data center. | +| image | A private image that has been uploaded to ProfitBricks. | +| snapshot | A snapshot of a storage volume. | +| ipblock | An IP block that has been reserved. | + +``` +GetResourceByType(resourcetype, resourceId) +``` + +--- + +## Example + +```go +package main + +import ( + "fmt" + "time" + + "github.com/profitbricks/profitbricks-sdk-go" +) + +func main() { + + //Sets username and password + profitbricks.SetAuth("username", "password") + //Sets depth. + profitbricks.SetDepth("5") + + dcrequest := profitbricks.Datacenter{ + Properties: profitbricks.DatacenterProperties{ + Name: "example.go3", + Description: "description", + Location: "us/lasdev", + }, + } + + datacenter := profitbricks.CreateDatacenter(dcrequest) + + serverrequest := profitbricks.Server{ + Properties: profitbricks.ServerProperties{ + Name: "go01", + Ram: 1024, + Cores: 2, + }, + } + server := profitbricks.CreateServer(datacenter.Id, serverrequest) + + volumerequest := profitbricks.Volume{ + Properties: profitbricks.VolumeProperties{ + Size: 1, + Name: "Volume Test", + LicenceType: "LINUX", + Type: "HDD", + }, + } + + storage := profitbricks.CreateVolume(datacenter.Id, volumerequest) + + serverupdaterequest := profitbricks.ServerProperties{ + Name: "go01renamed", + Cores: 1, + Ram: 256, + } + + profitbricks.PatchServer(datacenter.Id, server.Id, serverupdaterequest) + //It takes a moment for a volume to be provisioned so we wait. + time.Sleep(60 * time.Second) + + profitbricks.AttachVolume(datacenter.Id, server.Id, storage.Id) + + volumes := profitbricks.ListVolumes(datacenter.Id) + fmt.Println(volumes.Items) + servers := profitbricks.ListServers(datacenter.Id) + fmt.Println(servers.Items) + datacenters := profitbricks.ListDatacenters() + fmt.Println(datacenters.Items) + + profitbricks.DeleteServer(datacenter.Id, server.Id) + profitbricks.DeleteDatacenter(datacenter.Id) +} +``` + +# Support +You are welcome to contact us with questions or comments at [ProfitBricks DevOps Central](https://devops.profitbricks.com/). Please report any issues via [GitHub's issue tracker](https://github.com/profitbricks/profitbricks-sdk-go/issues). + +## Testing + +You can run all test by using the command `go test -timeout=120m` or run a single test by specifying the name of the test file `go test servers_test.go` + +## Contributing + +1. Fork it ( https://github.com/profitbricks/profitbricks-sdk-go/fork ) +2. Create your feature branch (`git checkout -b my-new-feature`) +3. Commit your changes (`git commit -am 'Add some feature'`) +4. Push to the branch (`git push origin my-new-feature`) +5. Create a new Pull Request diff --git a/vendor/github.com/profitbricks/profitbricks-sdk-go/config.go b/vendor/github.com/profitbricks/profitbricks-sdk-go/config.go index 567108951..368a111a0 100644 --- a/vendor/github.com/profitbricks/profitbricks-sdk-go/config.go +++ b/vendor/github.com/profitbricks/profitbricks-sdk-go/config.go @@ -1,7 +1,7 @@ package profitbricks -// Endpoint is the base url for REST requests . -var Endpoint = "https://api.profitbricks.com/rest/v2" +// Endpoint is the base url for REST requests. +var Endpoint = "https://api.profitbricks.com/cloudapi/v4" // Username for authentication . var Username string @@ -11,7 +11,9 @@ var Passwd string // SetEndpoint is used to set the REST Endpoint. Endpoint is declared in config.go func SetEndpoint(newendpoint string) string { - Endpoint = newendpoint + if newendpoint != "" { + Endpoint = newendpoint + } return Endpoint } @@ -21,3 +23,7 @@ func SetAuth(u, p string) { Username = u Passwd = p } + +func SetUserAgent(userAgent string) { + AgentHeader = userAgent +} diff --git a/vendor/github.com/profitbricks/profitbricks-sdk-go/contractresources.go b/vendor/github.com/profitbricks/profitbricks-sdk-go/contractresources.go new file mode 100644 index 000000000..cd4fa2c3c --- /dev/null +++ b/vendor/github.com/profitbricks/profitbricks-sdk-go/contractresources.go @@ -0,0 +1,60 @@ +package profitbricks + +import ( + "encoding/json" + "net/http" + "strconv" +) + +type ContractResources struct { + Id string `json:"id,omitempty"` + Type_ string `json:"type,omitempty"` + Href string `json:"href,omitempty"` + Properties ContractResourcesProperties `json:"properties,omitempty"` + Response string `json:"Response,omitempty"` + Headers *http.Header `json:"headers,omitempty"` + StatusCode int `json:"headers,omitempty"` +} + +type ContractResourcesProperties struct { + PBContractNumber string `json:"PB-Contract-Number,omitempty"` + Owner string `json:"owner,omitempty"` + Status string `json:"status,omitempty"` + ResourceLimits *ResourcesLimits `json:"resourceLimits,omitempty"` +} + +type ResourcesLimits struct { + CoresPerServer int32 `json:"coresPerServer,omitempty"` + CoresPerContract int32 `json:"coresPerContract,omitempty"` + CoresProvisioned int32 `json:"coresProvisioned,omitempty"` + RamPerServer int32 `json:"ramPerServer,omitempty"` + RamPerContract int32 `json:"ramPerContract,omitempty"` + RamProvisioned int32 `json:"ramProvisioned,omitempty"` + HddLimitPerVolume int64 `json:"hddLimitPerVolume,omitempty"` + HddLimitPerContract int64 `json:"hddLimitPerContract,omitempty"` + HddVolumeProvisioned int64 `json:"hddVolumeProvisioned,omitempty"` + SsdLimitPerVolume int64 `json:"ssdLimitPerVolume,omitempty"` + SsdLimitPerContract int64 `json:"ssdLimitPerContract,omitempty"` + SsdVolumeProvisioned int64 `json:"ssdVolumeProvisioned,omitempty"` + ReservableIps int32 `json:"reservableIps,omitempty"` + ReservedIpsOnContract int32 `json:"reservedIpsOnContract,omitempty"` + ReservedIpsInUse int32 `json:"reservedIpsInUse,omitempty"` +} + +func GetContractResources() ContractResources { + path := contract_resource_path() + url := mk_url(path) + `?depth=` + Depth + `&pretty=` + strconv.FormatBool(Pretty) + req, _ := http.NewRequest("GET", url, nil) + req.Header.Add("Content-Type", FullHeader) + resp := do(req) + return toContractResources(resp) +} + +func toContractResources(resp Resp) ContractResources { + var col ContractResources + json.Unmarshal(resp.Body, &col) + col.Response = string(resp.Body) + col.Headers = &resp.Headers + col.StatusCode = resp.StatusCode + return col +} diff --git a/vendor/github.com/profitbricks/profitbricks-sdk-go/datacenter.go b/vendor/github.com/profitbricks/profitbricks-sdk-go/datacenter.go index eb3d9094f..1bdf22d60 100644 --- a/vendor/github.com/profitbricks/profitbricks-sdk-go/datacenter.go +++ b/vendor/github.com/profitbricks/profitbricks-sdk-go/datacenter.go @@ -8,18 +8,18 @@ import ( ) type Datacenter struct { - Id string `json:"id,omitempty"` - Type_ string `json:"type,omitempty"` - Href string `json:"href,omitempty"` - Metadata *DatacenterElementMetadata `json:"metadata,omitempty"` - Properties DatacenterProperties `json:"properties,omitempty"` - Entities DatacenterEntities `json:"entities,omitempty"` - Response string `json:"Response,omitempty"` - Headers *http.Header `json:"headers,omitempty"` - StatusCode int `json:"headers,omitempty"` + Id string `json:"id,omitempty"` + Type_ string `json:"type,omitempty"` + Href string `json:"href,omitempty"` + Metadata *Metadata `json:"metadata,omitempty"` + Properties DatacenterProperties `json:"properties,omitempty"` + Entities DatacenterEntities `json:"entities,omitempty"` + Response string `json:"Response,omitempty"` + Headers *http.Header `json:"headers,omitempty"` + StatusCode int `json:"headers,omitempty"` } -type DatacenterElementMetadata struct { +type Metadata struct { CreatedDate time.Time `json:"createdDate,omitempty"` CreatedBy string `json:"createdBy,omitempty"` Etag string `json:"etag,omitempty"` diff --git a/vendor/github.com/profitbricks/profitbricks-sdk-go/firewallrule.go b/vendor/github.com/profitbricks/profitbricks-sdk-go/firewallrule.go index 11cae62a5..1d5ef4ec4 100644 --- a/vendor/github.com/profitbricks/profitbricks-sdk-go/firewallrule.go +++ b/vendor/github.com/profitbricks/profitbricks-sdk-go/firewallrule.go @@ -7,26 +7,26 @@ import ( ) type FirewallRule struct { - Id string `json:"id,omitempty"` - Type_ string `json:"type,omitempty"` - Href string `json:"href,omitempty"` - Metadata *DatacenterElementMetadata `json:"metadata,omitempty"` - Properties FirewallruleProperties `json:"properties,omitempty"` - Response string `json:"Response,omitempty"` - Headers *http.Header `json:"headers,omitempty"` - StatusCode int `json:"headers,omitempty"` + Id string `json:"id,omitempty"` + Type_ string `json:"type,omitempty"` + Href string `json:"href,omitempty"` + Metadata *Metadata `json:"metadata,omitempty"` + Properties FirewallruleProperties `json:"properties,omitempty"` + Response string `json:"Response,omitempty"` + Headers *http.Header `json:"headers,omitempty"` + StatusCode int `json:"headers,omitempty"` } type FirewallruleProperties struct { - Name string `json:"name,omitempty"` - Protocol string `json:"protocol,omitempty"` - SourceMac string `json:"sourceMac,omitempty"` - SourceIp string `json:"sourceIp,omitempty"` - TargetIp string `json:"targetIp,omitempty"` - IcmpCode int `json:"icmpCode,omitempty"` - IcmpType int `json:"icmpType,omitempty"` - PortRangeStart int `json:"portRangeStart,omitempty"` - PortRangeEnd int `json:"portRangeEnd,omitempty"` + Name string `json:"name,omitempty"` + Protocol string `json:"protocol,omitempty"` + SourceMac *string `json:"sourceMac,omitempty"` + SourceIp *string `json:"sourceIp,omitempty"` + TargetIp *string `json:"targetIp,omitempty"` + IcmpCode *int `json:"icmpCode,omitempty"` + IcmpType *int `json:"icmpType,omitempty"` + PortRangeStart *int `json:"portRangeStart,omitempty"` + PortRangeEnd *int `json:"portRangeEnd,omitempty"` } type FirewallRules struct { diff --git a/vendor/github.com/profitbricks/profitbricks-sdk-go/image.go b/vendor/github.com/profitbricks/profitbricks-sdk-go/image.go index 95ec9e25a..fa2ba2140 100644 --- a/vendor/github.com/profitbricks/profitbricks-sdk-go/image.go +++ b/vendor/github.com/profitbricks/profitbricks-sdk-go/image.go @@ -6,14 +6,14 @@ import ( ) type Image struct { - Id string `json:"id,omitempty"` - Type string `json:"type,omitempty"` - Href string `json:"href,omitempty"` - Metadata *DatacenterElementMetadata `json:"metadata,omitempty"` - Properties ImageProperties `json:"properties,omitempty"` - Response string `json:"Response,omitempty"` - Headers *http.Header `json:"headers,omitempty"` - StatusCode int `json:"headers,omitempty"` + Id string `json:"id,omitempty"` + Type string `json:"type,omitempty"` + Href string `json:"href,omitempty"` + Metadata *Metadata `json:"metadata,omitempty"` + Properties ImageProperties `json:"properties,omitempty"` + Response string `json:"Response,omitempty"` + Headers *http.Header `json:"headers,omitempty"` + StatusCode int `json:"headers,omitempty"` } type ImageProperties struct { diff --git a/vendor/github.com/profitbricks/profitbricks-sdk-go/ipblock.go b/vendor/github.com/profitbricks/profitbricks-sdk-go/ipblock.go index 1b78262bb..e6be130e2 100644 --- a/vendor/github.com/profitbricks/profitbricks-sdk-go/ipblock.go +++ b/vendor/github.com/profitbricks/profitbricks-sdk-go/ipblock.go @@ -7,17 +7,18 @@ import ( ) type IpBlock struct { - Id string `json:"id,omitempty"` - Type_ string `json:"type,omitempty"` - Href string `json:"href,omitempty"` - Metadata *DatacenterElementMetadata `json:"metadata,omitempty"` - Properties IpBlockProperties `json:"properties,omitempty"` - Response string `json:"Response,omitempty"` - Headers *http.Header `json:"headers,omitempty"` - StatusCode int `json:"headers,omitempty"` + Id string `json:"id,omitempty"` + Type_ string `json:"type,omitempty"` + Href string `json:"href,omitempty"` + Metadata *Metadata `json:"metadata,omitempty"` + Properties IpBlockProperties `json:"properties,omitempty"` + Response string `json:"Response,omitempty"` + Headers *http.Header `json:"headers,omitempty"` + StatusCode int `json:"headers,omitempty"` } type IpBlockProperties struct { + Name string `json:"name,omitempty"` Ips []string `json:"ips,omitempty"` Location string `json:"location,omitempty"` Size int `json:"size,omitempty"` diff --git a/vendor/github.com/profitbricks/profitbricks-sdk-go/lan.go b/vendor/github.com/profitbricks/profitbricks-sdk-go/lan.go index 2b7f85ed4..e855d80d6 100644 --- a/vendor/github.com/profitbricks/profitbricks-sdk-go/lan.go +++ b/vendor/github.com/profitbricks/profitbricks-sdk-go/lan.go @@ -6,27 +6,50 @@ import ( "net/http" ) -type Lan struct { - Id string `json:"id,omitempty"` - Type_ string `json:"type,omitempty"` - Href string `json:"href,omitempty"` - Metadata *DatacenterElementMetadata `json:"metadata,omitempty"` - Properties LanProperties `json:"properties,omitempty"` - Entities *LanEntities `json:"entities,omitempty"` - Response string `json:"Response,omitempty"` - Headers *http.Header `json:"headers,omitempty"` - StatusCode int `json:"headers,omitempty"` +type CreateLanRequest struct { + Id string `json:"id,omitempty"` + Type_ string `json:"type,omitempty"` + Href string `json:"href,omitempty"` + Metadata *Metadata `json:"metadata,omitempty"` + Properties CreateLanProperties `json:"properties,omitempty"` + Entities *LanEntities `json:"entities,omitempty"` + Response string `json:"Response,omitempty"` + Headers *http.Header `json:"headers,omitempty"` + StatusCode int `json:"headers,omitempty"` } -type LanProperties struct { +type CreateLanProperties struct { Name string `json:"name,omitempty"` Public bool `json:"public,omitempty"` } +type Lan struct { + Id string `json:"id,omitempty"` + Type_ string `json:"type,omitempty"` + Href string `json:"href,omitempty"` + Metadata *Metadata `json:"metadata,omitempty"` + Properties LanProperties `json:"properties,omitempty"` + Entities *LanEntities `json:"entities,omitempty"` + Response string `json:"Response,omitempty"` + Headers *http.Header `json:"headers,omitempty"` + StatusCode int `json:"headers,omitempty"` +} + +type LanProperties struct { + Name string `json:"name,omitempty"` + Public bool `json:"public,omitempty"` + IpFailover []IpFailover `json:"ipFailover"` +} + type LanEntities struct { Nics *LanNics `json:"nics,omitempty"` } +type IpFailover struct { + NicUuid string `json:"nicUuid,omitempty"` + Ip string `json:"ip,omitempty"` +} + type LanNics struct { Id string `json:"id,omitempty"` Type_ string `json:"type,omitempty"` @@ -55,7 +78,7 @@ func ListLans(dcid string) Lans { // CreateLan creates a lan in the datacenter // from a jason []byte and returns a Instance struct -func CreateLan(dcid string, request Lan) Lan { +func CreateLan(dcid string, request CreateLanRequest) Lan { obj, _ := json.Marshal(request) path := lan_col_path(dcid) url := mk_url(path) diff --git a/vendor/github.com/profitbricks/profitbricks-sdk-go/loadbalancer.go b/vendor/github.com/profitbricks/profitbricks-sdk-go/loadbalancer.go index 2a5b3389f..8d2f71ba6 100644 --- a/vendor/github.com/profitbricks/profitbricks-sdk-go/loadbalancer.go +++ b/vendor/github.com/profitbricks/profitbricks-sdk-go/loadbalancer.go @@ -7,15 +7,15 @@ import ( ) type Loadbalancer struct { - Id string `json:"id,omitempty"` - Type_ string `json:"type,omitempty"` - Href string `json:"href,omitempty"` - Metadata *DatacenterElementMetadata `json:"metadata,omitempty"` - Properties LoadbalancerProperties `json:"properties,omitempty"` - Entities LoadbalancerEntities `json:"entities,omitempty"` - Response string `json:"Response,omitempty"` - Headers *http.Header `json:"headers,omitempty"` - StatusCode int `json:"headers,omitempty"` + Id string `json:"id,omitempty"` + Type_ string `json:"type,omitempty"` + Href string `json:"href,omitempty"` + Metadata *Metadata `json:"metadata,omitempty"` + Properties LoadbalancerProperties `json:"properties,omitempty"` + Entities LoadbalancerEntities `json:"entities,omitempty"` + Response string `json:"Response,omitempty"` + Headers *http.Header `json:"headers,omitempty"` + StatusCode int `json:"headers,omitempty"` } type LoadbalancerProperties struct { diff --git a/vendor/github.com/profitbricks/profitbricks-sdk-go/location.go b/vendor/github.com/profitbricks/profitbricks-sdk-go/location.go index 279429464..c62f6e7cc 100644 --- a/vendor/github.com/profitbricks/profitbricks-sdk-go/location.go +++ b/vendor/github.com/profitbricks/profitbricks-sdk-go/location.go @@ -6,14 +6,14 @@ import ( ) type Location struct { - Id string `json:"id,omitempty"` - Type_ string `json:"type,omitempty"` - Href string `json:"href,omitempty"` - Metadata DatacenterElementMetadata `json:"metadata,omitempty"` - Properties Properties `json:"properties,omitempty"` - Response string `json:"Response,omitempty"` - Headers *http.Header `json:"headers,omitempty"` - StatusCode int `json:"headers,omitempty"` + Id string `json:"id,omitempty"` + Type_ string `json:"type,omitempty"` + Href string `json:"href,omitempty"` + Metadata Metadata `json:"metadata,omitempty"` + Properties LocationProperties `json:"properties,omitempty"` + Response string `json:"Response,omitempty"` + Headers *http.Header `json:"headers,omitempty"` + StatusCode int `json:"headers,omitempty"` } type Locations struct { @@ -26,8 +26,10 @@ type Locations struct { StatusCode int `json:"headers,omitempty"` } -type Properties struct { - Name string `json:"name,omitempty"` +type LocationProperties struct { + Name string `json:"name,omitempty"` + Features []string `json:"features,omitempty"` + ImageAliases []string `json:"imageAliases,omitempty"` } // ListLocations returns location collection data @@ -38,6 +40,14 @@ func ListLocations() Locations { return toLocations(do(req)) } +// GetRegionalLocations returns a list of available locations in a specific region +func GetRegionalLocations(regid string) Locations { + url := mk_url(location_reg_path(regid)) + `?depth=` + Depth + req, _ := http.NewRequest("GET", url, nil) + req.Header.Add("Content-Type", FullHeader) + return toLocations(do(req)) +} + // GetLocation returns location data func GetLocation(locid string) Location { url := mk_url(location_path(locid)) + `?depth=` + Depth diff --git a/vendor/github.com/profitbricks/profitbricks-sdk-go/nic.go b/vendor/github.com/profitbricks/profitbricks-sdk-go/nic.go index 74764f56b..3c0a0225c 100644 --- a/vendor/github.com/profitbricks/profitbricks-sdk-go/nic.go +++ b/vendor/github.com/profitbricks/profitbricks-sdk-go/nic.go @@ -7,24 +7,25 @@ import ( ) type Nic struct { - Id string `json:"id,omitempty"` - Type_ string `json:"type,omitempty"` - Href string `json:"href,omitempty"` - Metadata *DatacenterElementMetadata `json:"metadata,omitempty"` - Properties NicProperties `json:"properties,omitempty"` - Entities *NicEntities `json:"entities,omitempty"` - Response string `json:"Response,omitempty"` - Headers *http.Header `json:"headers,omitempty"` - StatusCode int `json:"headers,omitempty"` + Id string `json:"id,omitempty"` + Type_ string `json:"type,omitempty"` + Href string `json:"href,omitempty"` + Metadata *Metadata `json:"metadata,omitempty"` + Properties *NicProperties `json:"properties,omitempty"` + Entities *NicEntities `json:"entities,omitempty"` + Response string `json:"Response,omitempty"` + Headers *http.Header `json:"headers,omitempty"` + StatusCode int `json:"headers,omitempty"` } type NicProperties struct { Name string `json:"name,omitempty"` Mac string `json:"mac,omitempty"` Ips []string `json:"ips,omitempty"` - Dhcp bool `json:"dhcp,omitempty"` + Dhcp bool `json:"dhcp"` Lan int `json:"lan,omitempty"` FirewallActive bool `json:"firewallActive,omitempty"` + Nat bool `json:"nat,omitempty"` } type NicEntities struct { @@ -56,8 +57,8 @@ func ListNics(dcid, srvid string) Nics { // CreateNic creates a nic on a server // from a jason []byte and returns a Instance struct -func CreateNic(dcid string, srvid string, request Nic) Nic { - obj, _ := json.Marshal(request) +func CreateNic(dcid string, srvid string, nic Nic) Nic { + obj, _ := json.Marshal(nic) path := nic_col_path(dcid, srvid) url := mk_url(path) + `?depth=` + Depth req, _ := http.NewRequest("POST", url, bytes.NewBuffer(obj)) diff --git a/vendor/github.com/profitbricks/profitbricks-sdk-go/paths.go b/vendor/github.com/profitbricks/profitbricks-sdk-go/paths.go index 3b3c7f50d..f85f793aa 100644 --- a/vendor/github.com/profitbricks/profitbricks-sdk-go/paths.go +++ b/vendor/github.com/profitbricks/profitbricks-sdk-go/paths.go @@ -45,19 +45,9 @@ func location_path(locid string) string { return location_col_path() + slash(locid) } -// request_col_path returns the string "/requests" -func request_col_path() string { - return slash("requests") -} - -// request_path returns the string "/requests/" -func request_path(requestid string) string { - return request_col_path() + slash(requestid) -} - -// request_status_path returns the string "/requests/status" -func request_status_path(requestid string) string { - return request_path(requestid) + slash("status") +// location_path returns the string "/locations/" +func location_reg_path(regid string) string { + return location_col_path() + slash(regid) } // snapshot_col_path returns the string "/snapshots" @@ -65,11 +55,6 @@ func snapshot_col_path() string { return slash("snapshots") } -// snapshot_path returns the string "/snapshots/" -func snapshot_path(snapid string) string { - return snapshot_col_path() + slash(snapid) -} - // lan_col_path returns the string "/datacenters//lans" func lan_col_path(dcid string) string { return dc_path(dcid) + slash("lans") @@ -115,12 +100,6 @@ func volume_path(dcid, volid string) string { return volume_col_path(dcid) + slash(volid) } -// lan_nic_col_path returns the string /datacenters//lans//nics -func lan_nic_col(dcid, lanid string) string { - return lan_path(dcid, lanid) + slash("nics") - -} - // balnic_col_path returns the string "/loadbalancers//balancednics" func balnic_col_path(dcid, lbalid string) string { return lbal_path(dcid, lbalid) + slash("balancednics") @@ -171,3 +150,67 @@ func fwrule_col_path(dcid, srvid, nicid string) string { func fwrule_path(dcid, srvid, nicid, fwruleid string) string { return fwrule_col_path(dcid, srvid, nicid) + slash(fwruleid) } + +// contract_resource_path returns the string "/contracts" +func contract_resource_path() string { + return slash("contracts") +} + +func um() string { + return slash("um") +} + +// um_groups returns the string "/groups" +func um_groups() string { + return um() + slash("groups") +} + +// um_group_path returns the string "/groups/groupid" +func um_group_path(grpid string) string { + return um_groups() + slash(grpid) +} + +// um_group_shares returns the string "groups/{groupId}/shares" +func um_group_shares(grpid string) string { + return um() + slash("groups") + slash(grpid) + slash("shares") +} + +// um_group_share_path returns the string "groups/{groupId}/shares/{resourceId}" +func um_group_share_path(grpid string, resourceid string) string { + return um() + slash("groups") + slash(grpid) + slash("shares") + slash(resourceid) +} + +// um_group_users returns the string "/groups/groupid/users" +func um_group_users(grpid string) string { + return um() + slash("groups") + slash(grpid) + slash("users") +} + +// um_group_users_path returns the string "/groups/groupid/users/userid" +func um_group_users_path(grpid string, usrid string) string { + return um() + slash("groups") + slash(grpid) + slash("users") + slash(usrid) +} + +// um_users returns the string "/users" +func um_users() string { + return um() + slash("users") +} + +// um_users returns the string "/users/usrid" +func um_users_path(usrid string) string { + return um() + slash("users") + slash(usrid) +} + +// um_resources returns the string "/resources" +func um_resources() string { + return um() + slash("resources") +} + +// um_resources_type returns the string "/resources/resourceType" +func um_resources_type(restype string) string { + return um() + slash("resources") + slash(restype) +} + +// um_resources_type_path returns the string "resources/{resourceType}/{resourceId}" +func um_resources_type_path(restype string, resourceid string) string { + return um() + slash("resources") + slash(restype) + slash(resourceid) +} diff --git a/vendor/github.com/profitbricks/profitbricks-sdk-go/req.go b/vendor/github.com/profitbricks/profitbricks-sdk-go/req.go index b5f21c17d..893b23ca5 100644 --- a/vendor/github.com/profitbricks/profitbricks-sdk-go/req.go +++ b/vendor/github.com/profitbricks/profitbricks-sdk-go/req.go @@ -9,15 +9,18 @@ import ( ) //FullHeader is the standard header to include with all http requests except is_patch and is_command -const FullHeader = "application/vnd.profitbricks.resource+json" +const FullHeader = "application/json" + +var AgentHeader = "profitbricks-sdk-go/3.0.1" //PatchHeader is used with is_patch . -const PatchHeader = "application/vnd.profitbricks.partial-properties+json" +const PatchHeader = "application/json" //CommandHeader is used with is_command const CommandHeader = "application/x-www-form-urlencoded" var Depth = "5" +var Pretty = true // SetDepth is used to set Depth func SetDepth(newdepth string) string { @@ -25,6 +28,12 @@ func SetDepth(newdepth string) string { return Depth } +// SetDepth is used to set Depth +func SetPretty(pretty bool) bool { + Pretty = pretty + return Pretty +} + // mk_url either: // returns the path (if it`s a full url) // or @@ -32,7 +41,8 @@ func SetDepth(newdepth string) string { func mk_url(path string) string { if strings.HasPrefix(path, "http") { //REMOVE AFTER TESTING - path := strings.Replace(path, "https://api.profitbricks.com/rest/v2", Endpoint, 1) + //FIXME @jasmin Is this still relevant? + path := strings.Replace(path, "https://api.profitbricks.com/cloudapi/v3", Endpoint, 1) // END REMOVE return path } @@ -48,6 +58,7 @@ func mk_url(path string) string { func do(req *http.Request) Resp { client := &http.Client{} req.SetBasicAuth(Username, Passwd) + req.Header.Add("User-Agent", AgentHeader) resp, err := client.Do(req) if err != nil { panic(err) @@ -67,6 +78,7 @@ func is_delete(path string) Resp { url := mk_url(path) req, _ := http.NewRequest("DELETE", url, nil) req.Header.Add("Content-Type", FullHeader) + req.Header.Add("User-Agent", AgentHeader) return do(req) } @@ -76,5 +88,6 @@ func is_command(path string, jason string) Resp { body := json.RawMessage(jason) req, _ := http.NewRequest("POST", url, bytes.NewBuffer(body)) req.Header.Add("Content-Type", CommandHeader) + req.Header.Add("User-Agent", AgentHeader) return do(req) } diff --git a/vendor/github.com/profitbricks/profitbricks-sdk-go/request.go b/vendor/github.com/profitbricks/profitbricks-sdk-go/request.go index 023ae1a4a..6ce284d37 100644 --- a/vendor/github.com/profitbricks/profitbricks-sdk-go/request.go +++ b/vendor/github.com/profitbricks/profitbricks-sdk-go/request.go @@ -3,6 +3,7 @@ package profitbricks import ( "encoding/json" "net/http" + "time" ) type RequestStatus struct { @@ -26,6 +27,55 @@ type RequestTarget struct { Status string `json:"status,omitempty"` } +type Requests struct { + Id string `json:"id,omitempty"` + Type_ string `json:"type,omitempty"` + Href string `json:"href,omitempty"` + Items []Request `json:"items,omitempty"` + Response string `json:"Response,omitempty"` + Headers *http.Header `json:"headers,omitempty"` + StatusCode int `json:"headers,omitempty"` +} + +type Request struct { + ID string `json:"id"` + Type string `json:"type"` + Href string `json:"href"` + Metadata struct { + CreatedDate time.Time `json:"createdDate"` + CreatedBy string `json:"createdBy"` + Etag string `json:"etag"` + RequestStatus struct { + ID string `json:"id"` + Type string `json:"type"` + Href string `json:"href"` + } `json:"requestStatus"` + } `json:"metadata"` + Properties struct { + Method string `json:"method"` + Headers interface{} `json:"headers"` + Body interface{} `json:"body"` + URL string `json:"url"` + } `json:"properties"` + Response string `json:"Response,omitempty"` + Headers *http.Header `json:"headers,omitempty"` + StatusCode int `json:"headers,omitempty"` +} + +func ListRequests() Requests { + url := mk_url("/requests") + `?depth=` + Depth + req, _ := http.NewRequest("GET", url, nil) + req.Header.Add("Content-Type", FullHeader) + return toRequests(do(req)) +} + +func GetRequest(req_id string) Request { + url := mk_url("/requests/"+req_id) + `?depth=` + Depth + req, _ := http.NewRequest("GET", url, nil) + req.Header.Add("Content-Type", FullHeader) + return toRequest(do(req)) +} + func GetRequestStatus(path string) RequestStatus { url := mk_url(path) + `?depth=` + Depth req, _ := http.NewRequest("GET", url, nil) @@ -41,3 +91,21 @@ func toRequestStatus(resp Resp) RequestStatus { server.StatusCode = resp.StatusCode return server } + +func toRequests(resp Resp) Requests { + var server Requests + json.Unmarshal(resp.Body, &server) + server.Response = string(resp.Body) + server.Headers = &resp.Headers + server.StatusCode = resp.StatusCode + return server +} + +func toRequest(resp Resp) Request { + var server Request + json.Unmarshal(resp.Body, &server) + server.Response = string(resp.Body) + server.Headers = &resp.Headers + server.StatusCode = resp.StatusCode + return server +} diff --git a/vendor/github.com/profitbricks/profitbricks-sdk-go/resp.go b/vendor/github.com/profitbricks/profitbricks-sdk-go/resp.go index 12d68e0d2..b0449db37 100644 --- a/vendor/github.com/profitbricks/profitbricks-sdk-go/resp.go +++ b/vendor/github.com/profitbricks/profitbricks-sdk-go/resp.go @@ -15,13 +15,6 @@ func MkJson(i interface{}) string { return string(jason) } -// MetaData is a map for metadata returned in a Resp.Body -type StringMap map[string]string - -type StringIfaceMap map[string]interface{} - -type StringCollectionMap map[string]Collection - // Resp is the struct returned by all Rest request functions type Resp struct { Req *http.Request @@ -37,25 +30,3 @@ func (r *Resp) PrintHeaders() { } } - -type Id_Type_Href struct { - Id string `json:"id"` - Type string `json:"type"` - Href string `json:"href"` -} - -type MetaData StringIfaceMap - -type Instance struct { - Id_Type_Href - MetaData StringMap `json:"metaData,omitempty"` - Properties StringIfaceMap `json:"properties,omitempty"` - Entities StringCollectionMap `json:"entities,omitempty"` - Resp Resp `json:"-"` -} - -type Collection struct { - Id_Type_Href - Items []Instance `json:"items,omitempty"` - Resp Resp `json:"-"` -} diff --git a/vendor/github.com/profitbricks/profitbricks-sdk-go/server.go b/vendor/github.com/profitbricks/profitbricks-sdk-go/server.go index 256d57985..d876a0efd 100644 --- a/vendor/github.com/profitbricks/profitbricks-sdk-go/server.go +++ b/vendor/github.com/profitbricks/profitbricks-sdk-go/server.go @@ -3,20 +3,19 @@ package profitbricks import ( "bytes" "encoding/json" - "fmt" "net/http" ) type Server struct { - Id string `json:"id,omitempty"` - Type_ string `json:"type,omitempty"` - Href string `json:"href,omitempty"` - Metadata *DatacenterElementMetadata `json:"metadata,omitempty"` - Properties ServerProperties `json:"properties,omitempty"` - Entities *ServerEntities `json:"entities,omitempty"` - Response string `json:"Response,omitempty"` - Headers *http.Header `json:"headers,omitempty"` - StatusCode int `json:"headers,omitempty"` + Id string `json:"id,omitempty"` + Type_ string `json:"type,omitempty"` + Href string `json:"href,omitempty"` + Metadata *Metadata `json:"metadata,omitempty"` + Properties ServerProperties `json:"properties,omitempty"` + Entities *ServerEntities `json:"entities,omitempty"` + Response string `json:"Response,omitempty"` + Headers *http.Header `json:"headers,omitempty"` + StatusCode int `json:"headers,omitempty"` } type ServerProperties struct { @@ -81,7 +80,6 @@ func GetServer(dcid, srvid string) Server { path := server_path(dcid, srvid) url := mk_url(path) + `?depth=` + Depth req, _ := http.NewRequest("GET", url, nil) - fmt.Println(path) req.Header.Add("Content-Type", FullHeader) return toServer(do(req)) } diff --git a/vendor/github.com/profitbricks/profitbricks-sdk-go/snapshot.go b/vendor/github.com/profitbricks/profitbricks-sdk-go/snapshot.go index a5f54c04f..225414be3 100644 --- a/vendor/github.com/profitbricks/profitbricks-sdk-go/snapshot.go +++ b/vendor/github.com/profitbricks/profitbricks-sdk-go/snapshot.go @@ -7,14 +7,14 @@ import ( ) type Snapshot struct { - Id string `json:"id,omitempty"` - Type_ string `json:"type,omitempty"` - Href string `json:"href,omitempty"` - Metadata DatacenterElementMetadata `json:"metadata,omitempty"` - Properties SnapshotProperties `json:"properties,omitempty"` - Response string `json:"Response,omitempty"` - Headers *http.Header `json:"headers,omitempty"` - StatusCode int `json:"headers,omitempty"` + Id string `json:"id,omitempty"` + Type_ string `json:"type,omitempty"` + Href string `json:"href,omitempty"` + Metadata Metadata `json:"metadata,omitempty"` + Properties SnapshotProperties `json:"properties,omitempty"` + Response string `json:"Response,omitempty"` + Headers *http.Header `json:"headers,omitempty"` + StatusCode int `json:"headers,omitempty"` } type SnapshotProperties struct { diff --git a/vendor/github.com/profitbricks/profitbricks-sdk-go/test_helpers.go b/vendor/github.com/profitbricks/profitbricks-sdk-go/test_helpers.go index 35df1f929..c3a26137a 100644 --- a/vendor/github.com/profitbricks/profitbricks-sdk-go/test_helpers.go +++ b/vendor/github.com/profitbricks/profitbricks-sdk-go/test_helpers.go @@ -2,9 +2,48 @@ package profitbricks import ( "fmt" + "os" + "strings" "time" ) +func mkVolume(dcID string) string { + + var request = Volume{ + Properties: VolumeProperties{ + Size: 2, + Name: "Volume Test", + Type: "HDD", + ImagePassword: "test1234", + ImageAlias: "ubuntu:latest", + }, + } + + resp := CreateVolume(dcID, request) + waitTillProvisioned(resp.Headers.Get("Location")) + return resp.Id +} + +func mkipid(name string) string { + var obj = IpBlock{ + Properties: IpBlockProperties{ + Name: "GO SDK Test", + Size: 1, + Location: "us/las", + }, + } + + resp := ReserveIpBlock(obj) + return resp.Id +} + +func mksnapshotId(name string, dcId string) string { + svolumeId := mkVolume(dcId) + resp := CreateSnapshot(dcId, svolumeId, name, "description") + waitTillProvisioned(resp.Headers.Get("Location")) + return resp.Id +} + func mkdcid(name string) string { request := Datacenter{ Properties: DatacenterProperties{ @@ -14,11 +53,6 @@ func mkdcid(name string) string { }, } dc := CreateDatacenter(request) - fmt.Println("===========================") - fmt.Println("Created a DC " + name) - fmt.Println("Created a DC id " + dc.Id) - fmt.Println(dc.StatusCode) - fmt.Println("===========================") return dc.Id } @@ -31,42 +65,82 @@ func mksrvid(srv_dcid string) string { }, } srv := CreateServer(srv_dcid, req) - fmt.Println("===========================") - fmt.Println("Created a server " + srv.Id) - fmt.Println(srv.StatusCode) - fmt.Println("===========================") - waitTillProvisioned(srv.Headers.Get("Location")) return srv.Id } func mknic(lbal_dcid, serverid string) string { var request = Nic{ - Properties: NicProperties{ - Name: "GO SDK Original Nic", - Lan: 1, + Properties: &NicProperties{ + Lan: 1, + Name: "GO SDK Test", + Nat: false, + Dhcp: true, + FirewallActive: true, + Ips: []string{"10.0.0.1"}, }, } resp := CreateNic(lbal_dcid, serverid, request) - fmt.Println("===========================") - fmt.Println("created a nic at server " + serverid) + waitTillProvisioned(resp.Headers.Get("Location")) + return resp.Id +} - fmt.Println("created a nic with id " + resp.Id) - fmt.Println(resp.StatusCode) - fmt.Println("===========================") +func mknic_custom(lbal_dcid, serverid string, lanid int, ips []string) string { + var request = Nic{ + Properties: &NicProperties{ + Lan: lanid, + Name: "GO SDK Test", + Nat: false, + Dhcp: true, + FirewallActive: true, + Ips: ips, + }, + } + + resp := CreateNic(lbal_dcid, serverid, request) + waitTillProvisioned(resp.Headers.Get("Location")) return resp.Id } func waitTillProvisioned(path string) { - waitCount := 20 - fmt.Println(path) + waitCount := 120 for i := 0; i < waitCount; i++ { request := GetRequestStatus(path) if request.Metadata.Status == "DONE" { break } - time.Sleep(10 * time.Second) + time.Sleep(1 * time.Second) i++ } } + +func getImageId(location string, imageName string, imageType string) string { + if imageName == "" { + return "" + } + + SetAuth(os.Getenv("PROFITBRICKS_USERNAME"), os.Getenv("PROFITBRICKS_PASSWORD")) + + images := ListImages() + if images.StatusCode > 299 { + fmt.Printf("Error while fetching the list of images %s", images.Response) + } + + if len(images.Items) > 0 { + for _, i := range images.Items { + imgName := "" + if i.Properties.Name != "" { + imgName = i.Properties.Name + } + + if imageType == "SSD" { + imageType = "HDD" + } + if imgName != "" && strings.Contains(strings.ToLower(imgName), strings.ToLower(imageName)) && i.Properties.ImageType == imageType && i.Properties.Location == location && i.Properties.Public == true { + return i.Id + } + } + } + return "" +} diff --git a/vendor/github.com/profitbricks/profitbricks-sdk-go/usermanagment.go b/vendor/github.com/profitbricks/profitbricks-sdk-go/usermanagment.go new file mode 100644 index 000000000..fe7ef9b7d --- /dev/null +++ b/vendor/github.com/profitbricks/profitbricks-sdk-go/usermanagment.go @@ -0,0 +1,402 @@ +package profitbricks + +import ( + "bytes" + "encoding/json" + "net/http" + "strconv" +) + +type Groups struct { + Id string `json:"id,omitempty"` + Type_ string `json:"type,omitempty"` + Href string `json:"href,omitempty"` + Items []Group `json:"items,omitempty"` + Response string `json:"Response,omitempty"` + Headers *http.Header `json:"headers,omitempty"` + StatusCode int `json:"headers,omitempty"` +} + +type Group struct { + Id string `json:"id,omitempty"` + Type_ string `json:"type,omitempty"` + Href string `json:"href,omitempty"` + Properties GroupProperties `json:"properties,omitempty"` + Entities *GroupEntities `json:"entities,omitempty"` + Response string `json:"Response,omitempty"` + Headers *http.Header `json:"headers,omitempty"` + StatusCode int `json:"headers,omitempty"` +} + +type GroupProperties struct { + Name string `json:"name,omitempty"` + CreateDataCenter *bool `json:"createDataCenter,omitempty"` + CreateSnapshot *bool `json:"createSnapshot,omitempty"` + ReserveIp *bool `json:"reserveIp,omitempty"` + AccessActivityLog *bool `json:"accessActivityLog,omitempty"` +} + +type GroupEntities struct { + Users Users `json:"users,omitempty"` + Resources Resources `json:"resources,omitempty"` +} + +type Users struct { + Id string `json:"id,omitempty"` + Type_ string `json:"type,omitempty"` + Href string `json:"href,omitempty"` + Items []User `json:"items,omitempty"` + Response string `json:"Response,omitempty"` + Headers *http.Header `json:"headers,omitempty"` + StatusCode int `json:"headers,omitempty"` +} + +type User struct { + Id string `json:"id,omitempty"` + Type_ string `json:"type,omitempty"` + Href string `json:"href,omitempty"` + Metadata *Metadata `json:"metadata,omitempty"` + Properties *UserProperties `json:"properties,omitempty"` + Entities *UserEntities `json:"entities,omitempty"` + Response string `json:"Response,omitempty"` + Headers *http.Header `json:"headers,omitempty"` + StatusCode int `json:"headers,omitempty"` +} + +type UserProperties struct { + Firstname string `json:"firstname,omitempty"` + Lastname string `json:"lastname,omitempty"` + Email string `json:"email,omitempty"` + Password string `json:"password,omitempty"` + Administrator bool `json:"administrator,omitempty"` + ForceSecAuth bool `json:"forceSecAuth,omitempty"` + SecAuthActive bool `json:"secAuthActive,omitempty"` +} + +type UserEntities struct { + Groups Groups `json:"groups,omitempty"` + Owns Owns `json:"owns,omitempty"` +} + +type Resources struct { + Id string `json:"id,omitempty"` + Type_ string `json:"type,omitempty"` + Href string `json:"href,omitempty"` + Items []Resource `json:"items,omitempty"` + Response string `json:"Response,omitempty"` + Headers *http.Header `json:"headers,omitempty"` + StatusCode int `json:"headers,omitempty"` +} + +type Resource struct { + Id string `json:"id,omitempty"` + Type_ string `json:"type,omitempty"` + Href string `json:"href,omitempty"` + Metadata *Metadata `json:"metadata,omitempty"` + Entities *ResourceEntities `json:"entities,omitempty"` + Response string `json:"Response,omitempty"` + Headers *http.Header `json:"headers,omitempty"` + StatusCode int `json:"headers,omitempty"` +} + +type ResourceEntities struct { + Groups Groups `json:"groups,omitempty"` +} + +type Owns struct { + Id string `json:"id,omitempty"` + Type_ string `json:"type,omitempty"` + Href string `json:"href,omitempty"` + Items []Entity `json:"items,omitempty"` + Response string `json:"Response,omitempty"` + Headers *http.Header `json:"headers,omitempty"` + StatusCode int `json:"headers,omitempty"` +} + +type Entity struct { + Id string `json:"id,omitempty"` + Type_ string `json:"type,omitempty"` + Href string `json:"href,omitempty"` + Metadata *Metadata `json:"metadata,omitempty"` + Response string `json:"Response,omitempty"` + Headers *http.Header `json:"headers,omitempty"` + StatusCode int `json:"headers,omitempty"` +} + +type Shares struct { + Id string `json:"id,omitempty"` + Type_ string `json:"type,omitempty"` + Href string `json:"href,omitempty"` + Items []Share `json:"items,omitempty"` + Response string `json:"Response,omitempty"` + Headers *http.Header `json:"headers,omitempty"` + StatusCode int `json:"headers,omitempty"` +} + +type Share struct { + Id string `json:"id,omitempty"` + Type_ string `json:"type,omitempty"` + Href string `json:"href,omitempty"` + Properties ShareProperties `json:"properties,omitempty"` + Response string `json:"Response,omitempty"` + Headers *http.Header `json:"headers,omitempty"` + StatusCode int `json:"headers,omitempty"` +} + +type ShareProperties struct { + EditPrivilege *bool `json:"editPrivilege,omitempty"` + SharePrivilege *bool `json:"sharePrivilege,omitempty"` +} + +//Group fucntions +func ListGroups() Groups { + path := um_groups() + url := mk_url(path) + `?depth=` + Depth + `&pretty=` + strconv.FormatBool(Pretty) + req, _ := http.NewRequest("GET", url, nil) + req.Header.Add("Content-Type", FullHeader) + resp := do(req) + return toGroups(resp) +} + +func GetGroup(groupid string) Group { + path := um_group_path(groupid) + url := mk_url(path) + `?depth=` + Depth + `&pretty=` + strconv.FormatBool(Pretty) + req, _ := http.NewRequest("GET", url, nil) + req.Header.Add("Content-Type", FullHeader) + return toGroup(do(req)) +} + +func CreateGroup(grp Group) Group { + obj, _ := json.Marshal(grp) + path := um_groups() + url := mk_url(path) + req, _ := http.NewRequest("POST", url, bytes.NewBuffer(obj)) + req.Header.Add("Content-Type", FullHeader) + + return toGroup(do(req)) +} + +func UpdateGroup(groupid string, obj Group) Group { + jason_patch := []byte(MkJson(obj)) + path := um_group_path(groupid) + url := mk_url(path) + `?depth=` + Depth + req, _ := http.NewRequest("PUT", url, bytes.NewBuffer(jason_patch)) + req.Header.Add("Content-Type", PatchHeader) + return toGroup(do(req)) +} + +func DeleteGroup(groupid string) Resp { + path := um_group_path(groupid) + return is_delete(path) +} + +func toGroup(resp Resp) Group { + var grp Group + json.Unmarshal(resp.Body, &grp) + grp.Response = string(resp.Body) + grp.Headers = &resp.Headers + grp.StatusCode = resp.StatusCode + return grp +} + +func toGroups(resp Resp) Groups { + var col Groups + json.Unmarshal(resp.Body, &col) + col.Response = string(resp.Body) + col.Headers = &resp.Headers + col.StatusCode = resp.StatusCode + return col +} + +//Shares functions +func ListShares(grpid string) Shares { + path := um_group_shares(grpid) + url := mk_url(path) + `?depth=` + Depth + `&pretty=` + strconv.FormatBool(Pretty) + req, _ := http.NewRequest("GET", url, nil) + req.Header.Add("Content-Type", FullHeader) + resp := do(req) + return toShares(resp) +} + +func GetShare(groupid string, resourceid string) Share { + path := um_group_share_path(groupid, resourceid) + url := mk_url(path) + `?depth=` + Depth + `&pretty=` + strconv.FormatBool(Pretty) + req, _ := http.NewRequest("GET", url, nil) + req.Header.Add("Content-Type", FullHeader) + return toShare(do(req)) +} + +func AddShare(share Share, groupid string, resourceid string) Share { + obj, _ := json.Marshal(share) + path := um_group_share_path(groupid, resourceid) + url := mk_url(path) + req, _ := http.NewRequest("POST", url, bytes.NewBuffer(obj)) + req.Header.Add("Content-Type", FullHeader) + + return toShare(do(req)) +} + +func UpdateShare(groupid string, resourceid string, obj Share) Share { + jason_patch := []byte(MkJson(obj)) + path := um_group_share_path(groupid, resourceid) + url := mk_url(path) + `?depth=` + Depth + req, _ := http.NewRequest("PUT", url, bytes.NewBuffer(jason_patch)) + req.Header.Add("Content-Type", PatchHeader) + return toShare(do(req)) +} + +func DeleteShare(groupid string, resourceid string) Resp { + path := um_group_share_path(groupid, resourceid) + return is_delete(path) +} + +func toShare(resp Resp) Share { + var shr Share + json.Unmarshal(resp.Body, &shr) + shr.Response = string(resp.Body) + shr.Headers = &resp.Headers + shr.StatusCode = resp.StatusCode + return shr +} + +func toShares(resp Resp) Shares { + var col Shares + json.Unmarshal(resp.Body, &col) + col.Response = string(resp.Body) + col.Headers = &resp.Headers + col.StatusCode = resp.StatusCode + return col +} + +//Users in a group +func ListGroupUsers(groupid string) Users { + path := um_group_users(groupid) + url := mk_url(path) + `?depth=` + Depth + `&pretty=` + strconv.FormatBool(Pretty) + req, _ := http.NewRequest("GET", url, nil) + req.Header.Add("Content-Type", FullHeader) + resp := do(req) + return toUsers(resp) +} + +func AddUserToGroup(groupid string, userid string) User { + var usr User + usr.Id = userid + obj, _ := json.Marshal(usr) + path := um_group_users(groupid) + url := mk_url(path) + req, _ := http.NewRequest("POST", url, bytes.NewBuffer(obj)) + req.Header.Add("Content-Type", FullHeader) + + return toUser(do(req)) +} + +func DeleteUserFromGroup(groupid string, userid string) Resp { + path := um_group_users_path(groupid, userid) + return is_delete(path) +} + +func toUser(resp Resp) User { + var usr User + json.Unmarshal(resp.Body, &usr) + usr.Response = string(resp.Body) + usr.Headers = &resp.Headers + usr.StatusCode = resp.StatusCode + return usr +} + +func toUsers(resp Resp) Users { + var col Users + json.Unmarshal(resp.Body, &col) + col.Response = string(resp.Body) + col.Headers = &resp.Headers + col.StatusCode = resp.StatusCode + return col +} + +//Users +func ListUsers() Users { + path := um_users() + url := mk_url(path) + `?depth=` + Depth + `&pretty=` + strconv.FormatBool(Pretty) + req, _ := http.NewRequest("GET", url, nil) + req.Header.Add("Content-Type", FullHeader) + resp := do(req) + return toUsers(resp) +} + +func GetUser(usrid string) User { + path := um_users_path(usrid) + url := mk_url(path) + `?depth=` + Depth + `&pretty=` + strconv.FormatBool(Pretty) + req, _ := http.NewRequest("GET", url, nil) + req.Header.Add("Content-Type", FullHeader) + return toUser(do(req)) +} + +func CreateUser(usr User) User { + obj, _ := json.Marshal(usr) + path := um_users() + url := mk_url(path) + req, _ := http.NewRequest("POST", url, bytes.NewBuffer(obj)) + req.Header.Add("Content-Type", FullHeader) + + return toUser(do(req)) +} + +func UpdateUser(userid string, obj User) User { + jason_patch := []byte(MkJson(obj)) + path := um_users_path(userid) + url := mk_url(path) + req, _ := http.NewRequest("PUT", url, bytes.NewBuffer(jason_patch)) + req.Header.Add("Content-Type", PatchHeader) + return toUser(do(req)) +} + +func DeleteUser(groupid string) Resp { + path := um_users_path(groupid) + return is_delete(path) +} + +//Resources +func ListResources() Resources { + path := um_resources() + url := mk_url(path) + `?depth=` + Depth + `&pretty=` + strconv.FormatBool(Pretty) + req, _ := http.NewRequest("GET", url, nil) + req.Header.Add("Content-Type", FullHeader) + resp := do(req) + return toResources(resp) +} + +func GetResourceByType(resourcetype string, resourceid string) Resource { + path := um_resources_type_path(resourcetype, resourceid) + url := mk_url(path) + `?depth=` + Depth + `&pretty=` + strconv.FormatBool(Pretty) + req, _ := http.NewRequest("GET", url, nil) + req.Header.Add("Content-Type", FullHeader) + resp := do(req) + return toResource(resp) +} + +func ListResourcesByType(resourcetype string) Resources { + path := um_resources_type(resourcetype) + url := mk_url(path) + `?depth=` + Depth + `&pretty=` + strconv.FormatBool(Pretty) + req, _ := http.NewRequest("GET", url, nil) + req.Header.Add("Content-Type", FullHeader) + resp := do(req) + return toResources(resp) +} + +func toResources(resp Resp) Resources { + var col Resources + json.Unmarshal(resp.Body, &col) + col.Response = string(resp.Body) + col.Headers = &resp.Headers + col.StatusCode = resp.StatusCode + return col +} + +func toResource(resp Resp) Resource { + var col Resource + json.Unmarshal(resp.Body, &col) + col.Response = string(resp.Body) + col.Headers = &resp.Headers + col.StatusCode = resp.StatusCode + return col +} diff --git a/vendor/github.com/profitbricks/profitbricks-sdk-go/volume.go b/vendor/github.com/profitbricks/profitbricks-sdk-go/volume.go index 2982448d7..ebab8c4ee 100644 --- a/vendor/github.com/profitbricks/profitbricks-sdk-go/volume.go +++ b/vendor/github.com/profitbricks/profitbricks-sdk-go/volume.go @@ -4,24 +4,27 @@ import ( "bytes" "encoding/json" "net/http" + "net/url" ) type Volume struct { - Id string `json:"id,omitempty"` - Type_ string `json:"type,omitempty"` - Href string `json:"href,omitempty"` - Metadata *DatacenterElementMetadata `json:"metadata,omitempty"` - Properties VolumeProperties `json:"properties,omitempty"` - Response string `json:"Response,omitempty"` - Headers *http.Header `json:"headers,omitempty"` - StatusCode int `json:"headers,omitempty"` + Id string `json:"id,omitempty"` + Type_ string `json:"type,omitempty"` + Href string `json:"href,omitempty"` + Metadata *Metadata `json:"metadata,omitempty"` + Properties VolumeProperties `json:"properties,omitempty"` + Response string `json:"Response,omitempty"` + Headers *http.Header `json:"headers,omitempty"` + StatusCode int `json:"headers,omitempty"` } type VolumeProperties struct { Name string `json:"name,omitempty"` Type string `json:"type,omitempty"` Size int `json:"size,omitempty"` + AvailabilityZone string `json:"availabilityZone,omitempty"` Image string `json:"image,omitempty"` + ImageAlias string `json:"imageAlias,omitempty"` ImagePassword string `json:"imagePassword,omitempty"` SshKeys []string `json:"sshKeys,omitempty"` Bus string `json:"bus,omitempty"` @@ -95,12 +98,14 @@ func DeleteVolume(dcid, volid string) Resp { return is_delete(path) } -func CreateSnapshot(dcid string, volid string, name string) Snapshot { +func CreateSnapshot(dcid string, volid string, name string, description string) Snapshot { var path = volume_path(dcid, volid) path = path + "/create-snapshot" - url := mk_url(path) - body := json.RawMessage("name=" + name) - req, _ := http.NewRequest("POST", url, bytes.NewBuffer(body)) + req_url := mk_url(path) + data := url.Values{} + data.Set("name", name) + data.Add("description", description) + req, _ := http.NewRequest("POST", req_url, bytes.NewBufferString(data.Encode())) req.Header.Add("Content-Type", CommandHeader) return toSnapshot(do(req)) } diff --git a/vendor/vendor.json b/vendor/vendor.json index e1719e3e4..2c378680a 100644 --- a/vendor/vendor.json +++ b/vendor/vendor.json @@ -854,6 +854,12 @@ "path": "github.com/pmezard/go-difflib/difflib", "revision": "792786c7400a136282c1664665ae0a8db921c6c2" }, + { + "checksumSHA1": "Kq0fF7R65dDcGReuhf47O3LQgrY=", + "path": "github.com/profitbricks/profitbricks-sdk-go", + "revision": "7bdb11aecb0e457ea23c86898c6b49bfc0eb4bb1", + "revisionTime": "2017-08-01T13:52:49Z" + }, { "checksumSHA1": "TvF3ym5sZVNqGlUOS9HgOIl/sZM=", "path": "github.com/satori/go.uuid",