Skip to content

Commit

Permalink
change remote urls to inlude the cluster name
Browse files Browse the repository at this point in the history
  • Loading branch information
svenwiltink authored and fvanmaldegem committed Mar 15, 2023
1 parent d1192a8 commit f4faa82
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 149 deletions.
2 changes: 1 addition & 1 deletion configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

const (
libraryVersion = "6.19.1"
libraryVersion = "6.20.0"
defaultBasePath = "https://api.transip.nl/v6"
userAgent = "go-client-gotransip/" + libraryVersion
)
Expand Down
21 changes: 14 additions & 7 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ As of version 6.0 this package is no longer compatible with TransIP SOAP API bec
library is now organized around REST. The SOAP API library versions 5.* are now deprecated
and will no longer receive future updates.
Example
# Example
The following example uses the transip demo token in order to call the api with the test repository.
For more information about authenticating with your own credentials, see the Authentication section.
Expand Down Expand Up @@ -38,28 +37,31 @@ For more information about authenticating with your own credentials, see the Aut
log.Println("Test successful")
}
Authentication
# Authentication
If you want to tinker out with the api first without setting up your authentication,
we defined a static DemoClientConfiguration.
Which can be used to create a new client:
client, err := gotransip.NewClient(gotransip.DemoClientConfiguration)
Create a new client using a token:
client, err := gotransip.NewClient(gotransip.ClientConfiguration{
Token: "this_is_where_your_token_goes",
})
As tokens have a limited expiry time you can also request new tokens using the private key
acquired from your transip control panel:
client, err := gotransip.NewClient(gotransip.ClientConfiguration{
AccountName: "accountName",
PrivateKeyPath: "/path/to/api/private.key",
})
We also implemented a PrivateKeyReader option, for users that want to store their key elsewhere,
not on a filesystem but on X datastore:
file, err := os.Open("/path/to/api/private.key")
if err != nil {
panic(err.Error())
Expand All @@ -69,11 +71,11 @@ not on a filesystem but on X datastore:
PrivateKeyReader: file,
})
TokenCache
# TokenCache
If you would like to keep a token between multiple client instantiations,
you can provide the client with a token cache. If the file does not exist, it creates one for you
cache, err := authenticator.NewFileTokenCache("/tmp/path/to/gotransip_token_cache")
if err != nil {
panic(err.Error())
Expand All @@ -87,6 +89,7 @@ you can provide the client with a token cache. If the file does not exist, it cr
As long as a provided TokenCache adheres to the following interface,
the client's authenticator is able to use it. This means you can also provide
your own token cacher: for example, one that caches to etcd
type TokenCache interface {
// Set will save a token by name
Set(key string, token jwt.Token) error
Expand All @@ -95,18 +98,20 @@ your own token cacher: for example, one that caches to etcd
Get(key string) (jwt.Token, error)
}
Repositories
# Repositories
All resource calls as can be seen on https://api.transip.nl/rest/docs.html
have been grouped in the following repositories,
these are subpackages under the gotransip package:
availabilityzone.Repository
colocation.Repository
domain.Repository
haip.Repository
invoice.Repository
ipaddress.Repository
mailservice.Repository
kubernetes.Repository
product.Repository
test.Repository
traffic.Repository
Expand All @@ -115,10 +120,12 @@ these are subpackages under the gotransip package:
vps.Repository
Such a repository can be initialised with a client as follows:
domainRepo := domain.Repository{Client: client}
Each repository has a bunch methods you can use to call get/modify/update resources in
that specific subpackage. For example, here we get a list of domains from a transip account:
domains, err := domainRepo.GetAll()
*/
package gotransip
4 changes: 4 additions & 0 deletions kubernetes/blockstorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ type BlockStorage struct {
UUID string `json:"uuid"`
// User configurable unique identifier (max 64 chars)
Name string `json:"name"`
// ClusterName is the name of the cluster the blockstorage belongs to
ClusterName string `json:"clusterName"`
// The volume's size in gibibytes
SizeInGiB int `json:"sizeInGib"`
// Type of storage
Expand Down Expand Up @@ -67,6 +69,8 @@ const (
type BlockStorageOrder struct {
// user adjustable unique identifier for volume (max 64 chars), when none is given, the uuid will be used.
Name string `json:"name"`
// ClusterName name of the cluster the block storage will be available for
ClusterName string `json:"-"`
// amount of storage in gibibytes
SizeInGiB int `json:"sizeInGib"`
// type of storage
Expand Down
4 changes: 2 additions & 2 deletions kubernetes/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ type Cluster struct {
// URL to connect to with kubectl
Endpoint string `json:"endpoint"`
// Whether or not another process is already doing stuff with this cluster
IsLocked bool `json:"isLocked,omitempty"`
IsLocked bool `json:"isLocked"`
// If the cluster is administratively blocked
IsBlocked bool `json:"isBlocked,omitempty"`
IsBlocked bool `json:"isBlocked"`
}

// ClusterOrder struct can be used to order a new cluster
Expand Down
4 changes: 2 additions & 2 deletions kubernetes/nodepool.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type nodePoolWrapper struct {
type NodePool struct {
// The unique identifier for the node pool
UUID string `json:"uuid"`
// The name of the cluster the node pool should be ordered for
// The name of the cluster the node pool belongs to
ClusterName string `json:"clusterName"`
// The name that can be set by customer
Description string `json:"description"`
Expand All @@ -33,7 +33,7 @@ type NodePool struct {
// NodePoolOrder struct can be used to order a new node pool to a cluster
type NodePoolOrder struct {
// The name of the cluster the node pool should be ordered for
ClusterName string `json:"clusterName"`
ClusterName string `json:"-"`
// The description of the node pool
Description string `json:"description,omitempty"`
// Amount of desired nodes in the node pool
Expand Down
85 changes: 35 additions & 50 deletions kubernetes/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,165 +83,150 @@ func (r *Repository) GetKubeConfig(clusterName string) (string, error) {
}

// GetNodePools returns all node pools
func (r *Repository) GetNodePools() ([]NodePool, error) {
func (r *Repository) GetNodePools(clusterName string) ([]NodePool, error) {
var response nodePoolsWrapper
restRequest := rest.Request{Endpoint: "/kubernetes/node-pools"}
err := r.Client.Get(restRequest, &response)

return response.NodePools, err
}

// GetNodePoolsByClusterName returns all node pools for a given clusterName
func (r *Repository) GetNodePoolsByClusterName(clusterName string) ([]NodePool, error) {
var response nodePoolsWrapper
restRequest := rest.Request{Endpoint: "/kubernetes/node-pools", Parameters: url.Values{"clusterName": []string{clusterName}}}
restRequest := rest.Request{Endpoint: fmt.Sprintf("/kubernetes/clusters/%s/node-pools", clusterName)}
err := r.Client.Get(restRequest, &response)

return response.NodePools, err
}

// GetNodePool returns the NodePool for given nodePoolUUID
func (r *Repository) GetNodePool(nodePoolUUID string) (NodePool, error) {
func (r *Repository) GetNodePool(clusterName, nodePoolUUID string) (NodePool, error) {
var response nodePoolWrapper
restRequest := rest.Request{Endpoint: fmt.Sprintf("/kubernetes/node-pools/%s", nodePoolUUID)}
restRequest := rest.Request{Endpoint: fmt.Sprintf("/kubernetes/clusters/%s/node-pools/%s", clusterName, nodePoolUUID)}
err := r.Client.Get(restRequest, &response)

return response.NodePool, err
}

// AddNodePool allows you to order a new node pool to a cluster
func (r *Repository) AddNodePool(nodePoolOrder NodePoolOrder) error {
restRequest := rest.Request{Endpoint: "/kubernetes/node-pools", Body: &nodePoolOrder}
restRequest := rest.Request{Endpoint: fmt.Sprintf("/kubernetes/clusters/%s/node-pools", nodePoolOrder.ClusterName), Body: &nodePoolOrder}

return r.Client.Post(restRequest)
}

// UpdateNodePool allows you to update the description and desired node count of a node pool
func (r *Repository) UpdateNodePool(nodePool NodePool) error {
requestBody := nodePoolWrapper{NodePool: nodePool}
restRequest := rest.Request{Endpoint: fmt.Sprintf("/kubernetes/node-pools/%s", nodePool.UUID), Body: &requestBody}
restRequest := rest.Request{Endpoint: fmt.Sprintf("/kubernetes/clusters/%s/node-pools/%s", nodePool.ClusterName, nodePool.UUID), Body: &requestBody}

return r.Client.Put(restRequest)
}

// RemoveNodePool will cancel the node pool, thus deleting it
func (r *Repository) RemoveNodePool(nodePoolUUID string) error {
restRequest := rest.Request{Endpoint: fmt.Sprintf("/kubernetes/node-pools/%s", nodePoolUUID)}
func (r *Repository) RemoveNodePool(clusterName, nodePoolUUID string) error {
restRequest := rest.Request{Endpoint: fmt.Sprintf("/kubernetes/clusters/%s/node-pools/%s", clusterName, nodePoolUUID)}

return r.Client.Delete(restRequest)
}

// GetNodes returns all nodes
func (r *Repository) GetNodes() ([]Node, error) {
func (r *Repository) GetNodes(clusterName string) ([]Node, error) {
var response nodesWrapper
restRequest := rest.Request{Endpoint: "/kubernetes/nodes"}
err := r.Client.Get(restRequest, &response)

return response.Nodes, err
}

// GetNodesByClusterName returns all nodes for a cluster
func (r *Repository) GetNodesByClusterName(clusterName string) ([]Node, error) {
var response nodesWrapper
restRequest := rest.Request{Endpoint: "/kubernetes/nodes", Parameters: url.Values{"clusterName": []string{clusterName}}}
restRequest := rest.Request{Endpoint: fmt.Sprintf("/kubernetes/clusters/%s/nodes", clusterName)}
err := r.Client.Get(restRequest, &response)

return response.Nodes, err
}

// GetNodesByNodePoolUUID returns all nodes for a node pool
func (r *Repository) GetNodesByNodePoolUUID(nodePoolUUID string) ([]Node, error) {
func (r *Repository) GetNodesByNodePoolUUID(clusterName, nodePoolUUID string) ([]Node, error) {
var response nodesWrapper
restRequest := rest.Request{Endpoint: "/kubernetes/nodes", Parameters: url.Values{"nodePoolUuid": []string{nodePoolUUID}}}
restRequest := rest.Request{
Endpoint: fmt.Sprintf("/kubernetes/clusters/%s/nodes", clusterName),
Parameters: url.Values{"nodePoolUuid": []string{nodePoolUUID}},
}
err := r.Client.Get(restRequest, &response)

return response.Nodes, err
}

// GetNode return a node
func (r *Repository) GetNode(nodeUUID string) (Node, error) {
func (r *Repository) GetNode(clusterName, nodeUUID string) (Node, error) {
var response nodeWrapper
restRequest := rest.Request{Endpoint: fmt.Sprintf("/kubernetes/nodes/%s", nodeUUID)}
restRequest := rest.Request{Endpoint: fmt.Sprintf("/kubernetes/clusters/%s/nodes/%s", clusterName, nodeUUID)}
err := r.Client.Get(restRequest, &response)

return response.Node, err
}

// GetBlockStorageVolumes returns all block storage volumes
func (r *Repository) GetBlockStorageVolumes() ([]BlockStorage, error) {
func (r *Repository) GetBlockStorageVolumes(clusterName string) ([]BlockStorage, error) {
var response blockStoragesWrapper
restRequest := rest.Request{Endpoint: "/kubernetes/block-storages"}
restRequest := rest.Request{Endpoint: fmt.Sprintf("/kubernetes/clusters/%s/block-storages", clusterName)}
err := r.Client.Get(restRequest, &response)

return response.BlockStorages, err
}

// GetBlockStorageVolume returns a specific block storage volume
func (r *Repository) GetBlockStorageVolume(name string) (BlockStorage, error) {
func (r *Repository) GetBlockStorageVolume(clusterName, name string) (BlockStorage, error) {
var response blockStorageWrapper
restRequest := rest.Request{Endpoint: fmt.Sprintf("/kubernetes/block-storages/%s", name)}
restRequest := rest.Request{Endpoint: fmt.Sprintf("/kubernetes/clusters/%s/block-storages/%s", clusterName, name)}
err := r.Client.Get(restRequest, &response)

return response.BlockStorage, err
}

// AddBlockStorageVolume creates a block storage volume
func (r *Repository) AddBlockStorageVolume(order BlockStorageOrder) error {
restRequest := rest.Request{Endpoint: "/kubernetes/block-storages", Body: &order}
restRequest := rest.Request{Endpoint: fmt.Sprintf("/kubernetes/clusters/%s/block-storages", order.ClusterName), Body: &order}

return r.Client.Post(restRequest)
}

// UpdateBlockStorageVolume allows you to update the name and attached node for a block storage volumes
func (r *Repository) UpdateBlockStorageVolume(volume BlockStorage) error {
requestBody := blockStorageWrapper{BlockStorage: volume}
restRequest := rest.Request{Endpoint: fmt.Sprintf("/kubernetes/block-storages/%s", volume.Name), Body: &requestBody}
restRequest := rest.Request{Endpoint: fmt.Sprintf("/kubernetes/clusters/%s/block-storages/%s", volume.ClusterName, volume.Name), Body: &requestBody}

return r.Client.Put(restRequest)
}

// RemoveBlockStorageVolume will remove a block storage volume
func (r *Repository) RemoveBlockStorageVolume(name string) error {
restRequest := rest.Request{Endpoint: fmt.Sprintf("/kubernetes/block-storages/%s", name)}
func (r *Repository) RemoveBlockStorageVolume(clusterName, name string) error {
restRequest := rest.Request{Endpoint: fmt.Sprintf("/kubernetes/clusters/%s/block-storages/%s", clusterName, name)}

return r.Client.Delete(restRequest)
}

// GetLoadBalancers returns all load balancers
func (r *Repository) GetLoadBalancers() ([]LoadBalancer, error) {
func (r *Repository) GetLoadBalancers(clusterName string) ([]LoadBalancer, error) {
var response lbsWrapper
restRequest := rest.Request{Endpoint: "/kubernetes/load-balancers"}
restRequest := rest.Request{Endpoint: fmt.Sprintf("/kubernetes/clusters/%s/load-balancers", clusterName)}
err := r.Client.Get(restRequest, &response)

return response.LoadBalancers, err
}

// GetLoadBalancer returns a load balancer
func (r *Repository) GetLoadBalancer(name string) (LoadBalancer, error) {
func (r *Repository) GetLoadBalancer(clusterName, name string) (LoadBalancer, error) {
var response lbWrapper
restRequest := rest.Request{Endpoint: fmt.Sprintf("/kubernetes/load-balancers/%s", name)}
restRequest := rest.Request{Endpoint: fmt.Sprintf("/kubernetes/clusters/%s/load-balancers/%s", clusterName, name)}
err := r.Client.Get(restRequest, &response)

return response.LoadBalancer, err
}

// CreateLoadBalancer creates a new load balancer
func (r *Repository) CreateLoadBalancer(name string) error {
restRequest := rest.Request{Endpoint: "/kubernetes/load-balancers", Body: &lbOrder{Name: name}}
func (r *Repository) CreateLoadBalancer(clusterName, name string) error {
restRequest := rest.Request{Endpoint: fmt.Sprintf("/kubernetes/clusters/%s/load-balancers", clusterName), Body: &lbOrder{Name: name}}

return r.Client.Post(restRequest)
}

// UpdateLoadBalancer updates the entire state of the load balancer
func (r *Repository) UpdateLoadBalancer(name string, config LoadBalancerConfig) error {
restRequest := rest.Request{Endpoint: fmt.Sprintf("/kubernetes/load-balancers/%s", name), Body: &lbcWrapper{Config: config}}
func (r *Repository) UpdateLoadBalancer(clusterName, name string, config LoadBalancerConfig) error {
restRequest := rest.Request{Endpoint: fmt.Sprintf("/kubernetes/clusters/%s/load-balancers/%s", clusterName, name), Body: &lbcWrapper{Config: config}}

return r.Client.Put(restRequest)
}

// RemoveLoadBalancer will remove a load balancer
func (r *Repository) RemoveLoadBalancer(name string) error {
restRequest := rest.Request{Endpoint: fmt.Sprintf("/kubernetes/load-balancers/%s", name)}
func (r *Repository) RemoveLoadBalancer(clusterName, name string) error {
restRequest := rest.Request{Endpoint: fmt.Sprintf("/kubernetes/clusters/%s/load-balancers/%s", clusterName, name)}

return r.Client.Delete(restRequest)
}
Loading

0 comments on commit f4faa82

Please sign in to comment.