Commit 1439ea2b authored by Miao Luo's avatar Miao Luo

Update photon controller go SDK in vendor code.

parent 442e9200
...@@ -7131,13 +7131,18 @@ go_library( ...@@ -7131,13 +7131,18 @@ go_library(
) )
go_library( go_library(
name = "github.com/vmware/photon-controller-go-sdk/SSPI",
srcs = ["github.com/vmware/photon-controller-go-sdk/SSPI/sspi_unsupported.go"],
tags = ["automanaged"],
)
go_library(
name = "github.com/vmware/photon-controller-go-sdk/photon", name = "github.com/vmware/photon-controller-go-sdk/photon",
srcs = [ srcs = [
"github.com/vmware/photon-controller-go-sdk/photon/apitypes.go", "github.com/vmware/photon-controller-go-sdk/photon/apitypes.go",
"github.com/vmware/photon-controller-go-sdk/photon/auth.go", "github.com/vmware/photon-controller-go-sdk/photon/auth.go",
"github.com/vmware/photon-controller-go-sdk/photon/availabilityzones.go", "github.com/vmware/photon-controller-go-sdk/photon/availabilityzones.go",
"github.com/vmware/photon-controller-go-sdk/photon/client.go", "github.com/vmware/photon-controller-go-sdk/photon/client.go",
"github.com/vmware/photon-controller-go-sdk/photon/clusters.go",
"github.com/vmware/photon-controller-go-sdk/photon/deployments.go", "github.com/vmware/photon-controller-go-sdk/photon/deployments.go",
"github.com/vmware/photon-controller-go-sdk/photon/disks.go", "github.com/vmware/photon-controller-go-sdk/photon/disks.go",
"github.com/vmware/photon-controller-go-sdk/photon/flavors.go", "github.com/vmware/photon-controller-go-sdk/photon/flavors.go",
...@@ -7148,7 +7153,10 @@ go_library( ...@@ -7148,7 +7153,10 @@ go_library(
"github.com/vmware/photon-controller-go-sdk/photon/projects.go", "github.com/vmware/photon-controller-go-sdk/photon/projects.go",
"github.com/vmware/photon-controller-go-sdk/photon/resourcetickets.go", "github.com/vmware/photon-controller-go-sdk/photon/resourcetickets.go",
"github.com/vmware/photon-controller-go-sdk/photon/restclient.go", "github.com/vmware/photon-controller-go-sdk/photon/restclient.go",
"github.com/vmware/photon-controller-go-sdk/photon/routers.go",
"github.com/vmware/photon-controller-go-sdk/photon/services.go",
"github.com/vmware/photon-controller-go-sdk/photon/status.go", "github.com/vmware/photon-controller-go-sdk/photon/status.go",
"github.com/vmware/photon-controller-go-sdk/photon/subnets.go",
"github.com/vmware/photon-controller-go-sdk/photon/tasks.go", "github.com/vmware/photon-controller-go-sdk/photon/tasks.go",
"github.com/vmware/photon-controller-go-sdk/photon/tenants.go", "github.com/vmware/photon-controller-go-sdk/photon/tenants.go",
"github.com/vmware/photon-controller-go-sdk/photon/util.go", "github.com/vmware/photon-controller-go-sdk/photon/util.go",
...@@ -7164,6 +7172,7 @@ go_library( ...@@ -7164,6 +7172,7 @@ go_library(
srcs = [ srcs = [
"github.com/vmware/photon-controller-go-sdk/photon/lightwave/jwttoken.go", "github.com/vmware/photon-controller-go-sdk/photon/lightwave/jwttoken.go",
"github.com/vmware/photon-controller-go-sdk/photon/lightwave/oidcclient.go", "github.com/vmware/photon-controller-go-sdk/photon/lightwave/oidcclient.go",
"github.com/vmware/photon-controller-go-sdk/photon/lightwave/oidcclient_sspi_unsupported.go",
], ],
tags = ["automanaged"], tags = ["automanaged"],
) )
......
Copyright (c) 2012 The Go Authors. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\ No newline at end of file
SSPI implementation in this directory is taken from https://github.com/WillHipschman/sspi-prototype (git commit: 4feee8f2e75857bb51345fdfc8e3c091edc959b4)
Implementation is modified to make sure that it only compiles on Windows.
For non-Windows platforms, please look at sspi_unspported.go
// +build windows
package SSPI
import (
"fmt"
"strings"
"syscall"
"unsafe"
)
var (
secur32_dll = syscall.NewLazyDLL("secur32.dll")
initSecurityInterface = secur32_dll.NewProc("InitSecurityInterfaceW")
sec_fn *SecurityFunctionTable
)
func init() {
ptr, _, _ := initSecurityInterface.Call()
sec_fn = (*SecurityFunctionTable)(unsafe.Pointer(ptr))
}
const (
SEC_E_OK = 0
SECPKG_CRED_OUTBOUND = 2
SEC_WINNT_AUTH_IDENTITY_UNICODE = 2
ISC_REQ_DELEGATE = 0x00000001
ISC_REQ_REPLAY_DETECT = 0x00000004
ISC_REQ_SEQUENCE_DETECT = 0x00000008
ISC_REQ_CONFIDENTIALITY = 0x00000010
ISC_REQ_CONNECTION = 0x00000800
SECURITY_NETWORK_DREP = 0
SEC_I_CONTINUE_NEEDED = 0x00090312
SEC_I_COMPLETE_NEEDED = 0x00090313
SEC_I_COMPLETE_AND_CONTINUE = 0x00090314
SECBUFFER_VERSION = 0
SECBUFFER_TOKEN = 2
NTLMBUF_LEN = 12000
)
const ISC_REQ = ISC_REQ_CONFIDENTIALITY |
ISC_REQ_REPLAY_DETECT |
ISC_REQ_SEQUENCE_DETECT |
ISC_REQ_CONNECTION |
ISC_REQ_DELEGATE
type SecurityFunctionTable struct {
dwVersion uint32
EnumerateSecurityPackages uintptr
QueryCredentialsAttributes uintptr
AcquireCredentialsHandle uintptr
FreeCredentialsHandle uintptr
Reserved2 uintptr
InitializeSecurityContext uintptr
AcceptSecurityContext uintptr
CompleteAuthToken uintptr
DeleteSecurityContext uintptr
ApplyControlToken uintptr
QueryContextAttributes uintptr
ImpersonateSecurityContext uintptr
RevertSecurityContext uintptr
MakeSignature uintptr
VerifySignature uintptr
FreeContextBuffer uintptr
QuerySecurityPackageInfo uintptr
Reserved3 uintptr
Reserved4 uintptr
Reserved5 uintptr
Reserved6 uintptr
Reserved7 uintptr
Reserved8 uintptr
QuerySecurityContextToken uintptr
EncryptMessage uintptr
DecryptMessage uintptr
}
type SEC_WINNT_AUTH_IDENTITY struct {
User *uint16
UserLength uint32
Domain *uint16
DomainLength uint32
Password *uint16
PasswordLength uint32
Flags uint32
}
type TimeStamp struct {
LowPart uint32
HighPart int32
}
type SecHandle struct {
dwLower uintptr
dwUpper uintptr
}
type SecBuffer struct {
cbBuffer uint32
BufferType uint32
pvBuffer *byte
}
type SecBufferDesc struct {
ulVersion uint32
cBuffers uint32
pBuffers *SecBuffer
}
type SSPIAuth struct {
Domain string
UserName string
Password string
Service string
cred SecHandle
ctxt SecHandle
}
type Auth interface {
InitialBytes() ([]byte, error)
NextBytes([]byte) ([]byte, error)
Free()
}
// GetAuth returns SSPI auth object initialized with given params and true for success
// In case of error, it will return nil SSPI object and false for failure
func GetAuth(user, password, service, workstation string) (Auth, bool) {
if user == "" {
return &SSPIAuth{Service: service}, true
}
if !strings.ContainsRune(user, '\\') {
return nil, false
}
domain_user := strings.SplitN(user, "\\", 2)
return &SSPIAuth{
Domain: domain_user[0],
UserName: domain_user[1],
Password: password,
Service: service,
}, true
}
func (auth *SSPIAuth) InitialBytes() ([]byte, error) {
var identity *SEC_WINNT_AUTH_IDENTITY
if auth.UserName != "" {
identity = &SEC_WINNT_AUTH_IDENTITY{
Flags: SEC_WINNT_AUTH_IDENTITY_UNICODE,
Password: syscall.StringToUTF16Ptr(auth.Password),
PasswordLength: uint32(len(auth.Password)),
Domain: syscall.StringToUTF16Ptr(auth.Domain),
DomainLength: uint32(len(auth.Domain)),
User: syscall.StringToUTF16Ptr(auth.UserName),
UserLength: uint32(len(auth.UserName)),
}
}
var ts TimeStamp
sec_ok, _, _ := syscall.Syscall9(sec_fn.AcquireCredentialsHandle,
9,
0,
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr("Negotiate"))),
SECPKG_CRED_OUTBOUND,
0,
uintptr(unsafe.Pointer(identity)),
0,
0,
uintptr(unsafe.Pointer(&auth.cred)),
uintptr(unsafe.Pointer(&ts)))
if sec_ok != SEC_E_OK {
return nil, fmt.Errorf("AcquireCredentialsHandle failed %x", sec_ok)
}
var buf SecBuffer
var desc SecBufferDesc
desc.ulVersion = SECBUFFER_VERSION
desc.cBuffers = 1
desc.pBuffers = &buf
outbuf := make([]byte, NTLMBUF_LEN)
buf.cbBuffer = NTLMBUF_LEN
buf.BufferType = SECBUFFER_TOKEN
buf.pvBuffer = &outbuf[0]
var attrs uint32
sec_ok, _, _ = syscall.Syscall12(sec_fn.InitializeSecurityContext,
12,
uintptr(unsafe.Pointer(&auth.cred)),
0,
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(auth.Service))),
ISC_REQ,
0,
SECURITY_NETWORK_DREP,
0,
0,
uintptr(unsafe.Pointer(&auth.ctxt)),
uintptr(unsafe.Pointer(&desc)),
uintptr(unsafe.Pointer(&attrs)),
uintptr(unsafe.Pointer(&ts)))
if sec_ok == SEC_I_COMPLETE_AND_CONTINUE ||
sec_ok == SEC_I_COMPLETE_NEEDED {
syscall.Syscall6(sec_fn.CompleteAuthToken,
2,
uintptr(unsafe.Pointer(&auth.ctxt)),
uintptr(unsafe.Pointer(&desc)),
0, 0, 0, 0)
} else if sec_ok != SEC_E_OK &&
sec_ok != SEC_I_CONTINUE_NEEDED {
syscall.Syscall6(sec_fn.FreeCredentialsHandle,
1,
uintptr(unsafe.Pointer(&auth.cred)),
0, 0, 0, 0, 0)
return nil, fmt.Errorf("InitialBytes InitializeSecurityContext failed %x", sec_ok)
}
return outbuf[:buf.cbBuffer], nil
}
func (auth *SSPIAuth) NextBytes(bytes []byte) ([]byte, error) {
var in_buf, out_buf SecBuffer
var in_desc, out_desc SecBufferDesc
in_desc.ulVersion = SECBUFFER_VERSION
in_desc.cBuffers = 1
in_desc.pBuffers = &in_buf
out_desc.ulVersion = SECBUFFER_VERSION
out_desc.cBuffers = 1
out_desc.pBuffers = &out_buf
in_buf.BufferType = SECBUFFER_TOKEN
in_buf.pvBuffer = &bytes[0]
in_buf.cbBuffer = uint32(len(bytes))
outbuf := make([]byte, NTLMBUF_LEN)
out_buf.BufferType = SECBUFFER_TOKEN
out_buf.pvBuffer = &outbuf[0]
out_buf.cbBuffer = NTLMBUF_LEN
var attrs uint32
var ts TimeStamp
sec_ok, _, _ := syscall.Syscall12(sec_fn.InitializeSecurityContext,
12,
uintptr(unsafe.Pointer(&auth.cred)),
uintptr(unsafe.Pointer(&auth.ctxt)),
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(auth.Service))),
ISC_REQ,
0,
SECURITY_NETWORK_DREP,
uintptr(unsafe.Pointer(&in_desc)),
0,
uintptr(unsafe.Pointer(&auth.ctxt)),
uintptr(unsafe.Pointer(&out_desc)),
uintptr(unsafe.Pointer(&attrs)),
uintptr(unsafe.Pointer(&ts)))
if sec_ok == SEC_I_COMPLETE_AND_CONTINUE ||
sec_ok == SEC_I_COMPLETE_NEEDED {
syscall.Syscall6(sec_fn.CompleteAuthToken,
2,
uintptr(unsafe.Pointer(&auth.ctxt)),
uintptr(unsafe.Pointer(&out_desc)),
0, 0, 0, 0)
} else if sec_ok != SEC_E_OK &&
sec_ok != SEC_I_CONTINUE_NEEDED {
return nil, fmt.Errorf("NextBytes InitializeSecurityContext failed %x", sec_ok)
}
return outbuf[:out_buf.cbBuffer], nil
}
func (auth *SSPIAuth) Free() {
syscall.Syscall6(sec_fn.DeleteSecurityContext,
1,
uintptr(unsafe.Pointer(&auth.ctxt)),
0, 0, 0, 0, 0)
syscall.Syscall6(sec_fn.FreeCredentialsHandle,
1,
uintptr(unsafe.Pointer(&auth.cred)),
0, 0, 0, 0, 0)
}
// Copyright (c) 2017 VMware, Inc. All Rights Reserved.
//
// This product is licensed to you under the Apache License, Version 2.0 (the "License").
// You may not use this product except in compliance with the License.
//
// This product may include a number of subcomponents with separate copyright notices and
// license terms. Your use of these subcomponents is subject to the terms and conditions
// of the subcomponent's license, as noted in the LICENSE file.
// +build !windows
package SSPI
...@@ -480,7 +480,7 @@ type Deployment struct { ...@@ -480,7 +480,7 @@ type Deployment struct {
ImageDatastores []string `json:"imageDatastores"` ImageDatastores []string `json:"imageDatastores"`
SelfLink string `json:"selfLink"` SelfLink string `json:"selfLink"`
Migration *MigrationStatus `json:"migrationStatus,omitempty"` Migration *MigrationStatus `json:"migrationStatus,omitempty"`
ClusterConfigurations []ClusterConfiguration `json:"clusterConfigurations,omitempty"` ServiceConfigurations []ServiceConfiguration `json:"serviceConfigurations,omitempty"`
LoadBalancerEnabled bool `json:"loadBalancerEnabled"` LoadBalancerEnabled bool `json:"loadBalancerEnabled"`
LoadBalancerAddress string `json:"loadBalancerAddress"` LoadBalancerAddress string `json:"loadBalancerAddress"`
} }
...@@ -514,7 +514,6 @@ type AuthInfo struct { ...@@ -514,7 +514,6 @@ type AuthInfo struct {
Tenant string `json:"tenant,omitempty"` Tenant string `json:"tenant,omitempty"`
Port int `json:"port,omitempty"` Port int `json:"port,omitempty"`
SecurityGroups []string `json:"securityGroups,omitempty"` SecurityGroups []string `json:"securityGroups,omitempty"`
Enabled bool `json:"enabled,omitempty"`
Username string `json:"username,omitempty"` Username string `json:"username,omitempty"`
} }
...@@ -532,6 +531,8 @@ type NetworkConfigurationCreateSpec struct { ...@@ -532,6 +531,8 @@ type NetworkConfigurationCreateSpec struct {
Password string `json:"networkManagerPassword,omitempty"` Password string `json:"networkManagerPassword,omitempty"`
NetworkZoneId string `json:"networkZoneId,omitempty"` NetworkZoneId string `json:"networkZoneId,omitempty"`
TopRouterId string `json:"networkTopRouterId,omitempty"` TopRouterId string `json:"networkTopRouterId,omitempty"`
EdgeIpPoolId string `json:"networkEdgeIpPoolId,omitempty"`
HostUplinkPnic string `json:"networkHostUplinkPnic,omitempty"`
IpRange string `json:"ipRange,omitempty"` IpRange string `json:"ipRange,omitempty"`
ExternalIpRange *IpRange `json:"externalIpRange,omitempty"` ExternalIpRange *IpRange `json:"externalIpRange,omitempty"`
DhcpServers []string `json:"dhcpServers,omitempty"` DhcpServers []string `json:"dhcpServers,omitempty"`
...@@ -545,21 +546,23 @@ type NetworkConfiguration struct { ...@@ -545,21 +546,23 @@ type NetworkConfiguration struct {
Password string `json:"networkManagerPassword,omitempty"` Password string `json:"networkManagerPassword,omitempty"`
NetworkZoneId string `json:"networkZoneId,omitempty"` NetworkZoneId string `json:"networkZoneId,omitempty"`
TopRouterId string `json:"networkTopRouterId,omitempty"` TopRouterId string `json:"networkTopRouterId,omitempty"`
EdgeIpPoolId string `json:"networkEdgeIpPoolId,omitempty"`
HostUplinkPnic string `json:"networkHostUplinkPnic,omitempty"`
IpRange string `json:"ipRange,omitempty"` IpRange string `json:"ipRange,omitempty"`
FloatingIpRange *IpRange `json:"floatingIpRange,omitempty"` FloatingIpRange *IpRange `json:"floatingIpRange,omitempty"`
SnatIp string `json:"snatIp,omitempty"` SnatIp string `json:"snatIp,omitempty"`
DhcpServers []string `json:"dhcpServers,omitempty"` DhcpServers []string `json:"dhcpServers,omitempty"`
} }
// Creation spec for subnets. // Creation spec for networks.
type SubnetCreateSpec struct { type NetworkCreateSpec struct {
Name string `json:"name"` Name string `json:"name"`
Description string `json:"description,omitempty"` Description string `json:"description,omitempty"`
PortGroups []string `json:"portGroups"` PortGroups []string `json:"portGroups"`
} }
// Represents a subnet // Represents a subnet
type Subnet struct { type Network struct {
Kind string `json:"kind"` Kind string `json:"kind"`
Name string `json:"name"` Name string `json:"name"`
Description string `json:"description,omitempty"` Description string `json:"description,omitempty"`
...@@ -572,8 +575,8 @@ type Subnet struct { ...@@ -572,8 +575,8 @@ type Subnet struct {
} }
// Represents multiple subnets returned by the API // Represents multiple subnets returned by the API
type Subnets struct { type Networks struct {
Items []Subnet `json:"items"` Items []Network `json:"items"`
} }
// Create spec for virtual subnet // Create spec for virtual subnet
...@@ -592,7 +595,7 @@ type VirtualSubnet struct { ...@@ -592,7 +595,7 @@ type VirtualSubnet struct {
Description string `json:"description,omitempty"` Description string `json:"description,omitempty"`
State string `json:"state"` State string `json:"state"`
RoutingType string `json:"routingType"` RoutingType string `json:"routingType"`
IsDefault string `json:"isDefault"` IsDefault bool `json:"isDefault"`
Cidr string `json:"cidr,omitempty"` Cidr string `json:"cidr,omitempty"`
LowIpDynamic string `json:"lowIpDynamic,omitempty"` LowIpDynamic string `json:"lowIpDynamic,omitempty"`
HighIpDynamic string `json:"highIpDynamic,omitempty"` HighIpDynamic string `json:"highIpDynamic,omitempty"`
...@@ -607,33 +610,59 @@ type VirtualSubnets struct { ...@@ -607,33 +610,59 @@ type VirtualSubnets struct {
Items []VirtualSubnet `json:"items"` Items []VirtualSubnet `json:"items"`
} }
// Creation spec for Cluster Configuration. // Represents a router
type ClusterConfigurationSpec struct { type Router struct {
ID string `json:"id"`
Kind string `json:"kind"`
Name string `json:"name"`
PrivateIpCidr string `json:"privateIpCidr"`
}
// Represents multiple routers returned by the API.
type Routers struct {
Items []Router `json:"items"`
}
type RouterCreateSpec struct {
Name string `json:"name"`
PrivateIpCidr string `json:"privateIpCidr"`
}
// Represents name that can be set for router
type RouterUpdateSpec struct {
RouterName string `json:"name"`
}
// Creation spec for Service Configuration.
type ServiceConfigurationSpec struct {
Type string `json:"type"` Type string `json:"type"`
ImageID string `json:"imageId"` ImageID string `json:"imageId"`
} }
// Represnts a Cluster configuration. // Represnts a Service configuration.
type ClusterConfiguration struct { type ServiceConfiguration struct {
Kind string `json:"kind"` Kind string `json:"kind"`
Type string `json:"type"` Type string `json:"type"`
ImageID string `json:"imageId"` ImageID string `json:"imageId"`
} }
// Creation spec for clusters. // Creation spec for services.
type ClusterCreateSpec struct { type ServiceCreateSpec struct {
Name string `json:"name"` Name string `json:"name"`
Type string `json:"type"` Type string `json:"type"`
VMFlavor string `json:"vmFlavor,omitempty"` VMFlavor string `json:"vmFlavor,omitempty"`
MasterVmFlavor string `json:"masterVmFlavor,omitempty"`
WorkerVmFlavor string `json:"workerVmFlavor,omitempty"`
DiskFlavor string `json:"diskFlavor,omitempty"` DiskFlavor string `json:"diskFlavor,omitempty"`
NetworkID string `json:"vmNetworkId,omitempty"` NetworkID string `json:"vmNetworkId,omitempty"`
ImageID string `json:"imageId,omitempty"`
WorkerCount int `json:"workerCount"` WorkerCount int `json:"workerCount"`
BatchSizeWorker int `json:"workerBatchExpansionSize,omitempty"` BatchSizeWorker int `json:"workerBatchExpansionSize,omitempty"`
ExtendedProperties map[string]string `json:"extendedProperties"` ExtendedProperties map[string]string `json:"extendedProperties"`
} }
// Represents a cluster // Represents a service
type Cluster struct { type Service struct {
Kind string `json:"kind"` Kind string `json:"kind"`
Name string `json:"name"` Name string `json:"name"`
State string `json:"state"` State string `json:"state"`
...@@ -646,16 +675,21 @@ type Cluster struct { ...@@ -646,16 +675,21 @@ type Cluster struct {
ExtendedProperties map[string]string `json:"extendedProperties"` ExtendedProperties map[string]string `json:"extendedProperties"`
} }
// Represents multiple clusters returned by the API // Represents multiple services returned by the API
type Clusters struct { type Services struct {
Items []Cluster `json:"items"` Items []Service `json:"items"`
} }
// Represents cluster size that can be resized for cluster // Represents service size that can be resized for service
type ClusterResizeOperation struct { type ServiceResizeOperation struct {
NewWorkerCount int `json:"newWorkerCount"` NewWorkerCount int `json:"newWorkerCount"`
} }
// Represents service imageId that can be updated during change version
type ServiceChangeVersionOperation struct {
NewImageID string `json:"newImageId"`
}
// Represents a security group // Represents a security group
type SecurityGroup struct { type SecurityGroup struct {
Name string `json:"name"` Name string `json:"name"`
...@@ -709,3 +743,60 @@ type Info struct { ...@@ -709,3 +743,60 @@ type Info struct {
GitCommitHash string `json:"gitCommitHash"` GitCommitHash string `json:"gitCommitHash"`
NetworkType string `json:"networkType"` NetworkType string `json:"networkType"`
} }
// NSX configuration spec
type NsxConfigurationSpec struct {
NsxAddress string `json:"nsxAddress"`
NsxUsername string `json:"nsxUsername"`
NsxPassword string `json:"nsxPassword"`
DhcpServerAddresses map[string]string `json:"dhcpServerAddresses"`
PrivateIpRootCidr string `json:"privateIpRootCidr"`
FloatingIpRootRange IpRange `json:"floatingIpRootRange"`
T0RouterId string `json:"t0RouterId"`
EdgeClusterId string `json:"edgeClusterId"`
OverlayTransportZoneId string `json:"overlayTransportZoneId"`
TunnelIpPoolId string `json:"tunnelIpPoolId"`
HostUplinkPnic string `json:"hostUplinkPnic"`
HostUplinkVlanId int `json:"hostUplinkVlanId"`
}
// Represents a subnet
type Subnet struct {
ID string `json:"id"`
Kind string `json:"kind"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
PrivateIpCidr string `json:"privateIpCidr"`
ReservedIps map[string]string `json:"reservedIps"`
State string `json:"state"`
}
// Represents multiple subnets returned by the API.
type Subnets struct {
Items []Subnet `json:"items"`
}
// Creation spec for subnets.
type SubnetCreateSpec struct {
Name string `json:"name"`
Description string `json:"description"`
PrivateIpCidr string `json:"privateIpCidr"`
}
// Represents name that can be set for subnet
type SubnetUpdateSpec struct {
SubnetName string `json:"name"`
}
// Identity and Access Management (IAM)
// IAM Policy entry
type PolicyEntry struct {
Principal string `json:"principal"`
Roles []string `json:"roles"`
}
type PolicyDelta struct {
Principal string `json:"principal"`
Action string `json:"action"`
Role string `json:"role"`
}
...@@ -12,6 +12,7 @@ package photon ...@@ -12,6 +12,7 @@ package photon
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/vmware/photon-controller-go-sdk/photon/lightwave" "github.com/vmware/photon-controller-go-sdk/photon/lightwave"
) )
...@@ -20,11 +21,11 @@ type AuthAPI struct { ...@@ -20,11 +21,11 @@ type AuthAPI struct {
client *Client client *Client
} }
const authUrl string = "/auth" const authUrl string = rootUrl + "/auth"
// Gets authentication info. // Gets authentication info.
func (api *AuthAPI) Get() (info *AuthInfo, err error) { func (api *AuthAPI) Get() (info *AuthInfo, err error) {
res, err := api.client.restClient.Get(api.client.Endpoint+authUrl, "") res, err := api.client.restClient.Get(api.client.Endpoint+authUrl, nil)
if err != nil { if err != nil {
return return
} }
...@@ -53,6 +54,22 @@ func (api *AuthAPI) GetTokensByPassword(username string, password string) (token ...@@ -53,6 +54,22 @@ func (api *AuthAPI) GetTokensByPassword(username string, password string) (token
return api.toTokenOptions(tokenResponse), nil return api.toTokenOptions(tokenResponse), nil
} }
// GetTokensFromWindowsLogInContext gets tokens based on Windows logged in context
// In case of running on platform other than Windows, it returns error
func (api *AuthAPI) GetTokensFromWindowsLogInContext() (tokenOptions *TokenOptions, err error) {
oidcClient, err := api.buildOIDCClient()
if err != nil {
return
}
tokenResponse, err := oidcClient.GetTokensFromWindowsLogInContext()
if err != nil {
return
}
return api.toTokenOptions(tokenResponse), nil
}
// Gets tokens from refresh token. // Gets tokens from refresh token.
func (api *AuthAPI) GetTokensByRefreshToken(refreshtoken string) (tokenOptions *TokenOptions, err error) { func (api *AuthAPI) GetTokensByRefreshToken(refreshtoken string) (tokenOptions *TokenOptions, err error) {
oidcClient, err := api.buildOIDCClient() oidcClient, err := api.buildOIDCClient()
...@@ -74,10 +91,6 @@ func (api *AuthAPI) getAuthEndpoint() (endpoint string, err error) { ...@@ -74,10 +91,6 @@ func (api *AuthAPI) getAuthEndpoint() (endpoint string, err error) {
return return
} }
if !authInfo.Enabled {
return "", SdkError{Message: "Authentication not enabled on this endpoint"}
}
if authInfo.Port == 0 { if authInfo.Port == 0 {
authInfo.Port = 443 authInfo.Port = 443
} }
...@@ -97,7 +110,7 @@ func (api *AuthAPI) buildOIDCClient() (client *lightwave.OIDCClient, err error) ...@@ -97,7 +110,7 @@ func (api *AuthAPI) buildOIDCClient() (client *lightwave.OIDCClient, err error)
api.client.restClient.logger), nil api.client.restClient.logger), nil
} }
const tokenScope string = "openid offline_access rs_esxcloud at_groups" const tokenScope string = "openid offline_access rs_photon_platform at_groups"
func (api *AuthAPI) buildOIDCClientOptions(options *ClientOptions) *lightwave.OIDCClientOptions { func (api *AuthAPI) buildOIDCClientOptions(options *ClientOptions) *lightwave.OIDCClientOptions {
return &lightwave.OIDCClientOptions{ return &lightwave.OIDCClientOptions{
......
...@@ -19,7 +19,7 @@ type AvailabilityZonesAPI struct { ...@@ -19,7 +19,7 @@ type AvailabilityZonesAPI struct {
client *Client client *Client
} }
var availabilityzoneUrl string = "/availabilityzones" var availabilityzoneUrl string = rootUrl + "/availabilityzones"
// Creates availability zone. // Creates availability zone.
func (api *AvailabilityZonesAPI) Create(availabilityzoneSpec *AvailabilityZoneCreateSpec) (task *Task, err error) { func (api *AvailabilityZonesAPI) Create(availabilityzoneSpec *AvailabilityZoneCreateSpec) (task *Task, err error) {
...@@ -31,7 +31,7 @@ func (api *AvailabilityZonesAPI) Create(availabilityzoneSpec *AvailabilityZoneCr ...@@ -31,7 +31,7 @@ func (api *AvailabilityZonesAPI) Create(availabilityzoneSpec *AvailabilityZoneCr
api.client.Endpoint+availabilityzoneUrl, api.client.Endpoint+availabilityzoneUrl,
"application/json", "application/json",
bytes.NewReader(body), bytes.NewReader(body),
api.client.options.TokenOptions.AccessToken) api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -42,7 +42,7 @@ func (api *AvailabilityZonesAPI) Create(availabilityzoneSpec *AvailabilityZoneCr ...@@ -42,7 +42,7 @@ func (api *AvailabilityZonesAPI) Create(availabilityzoneSpec *AvailabilityZoneCr
// Gets availability zone with the specified ID. // Gets availability zone with the specified ID.
func (api *AvailabilityZonesAPI) Get(id string) (availabilityzone *AvailabilityZone, err error) { func (api *AvailabilityZonesAPI) Get(id string) (availabilityzone *AvailabilityZone, err error) {
res, err := api.client.restClient.Get(api.getEntityUrl(id), api.client.options.TokenOptions.AccessToken) res, err := api.client.restClient.Get(api.getEntityUrl(id), api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -59,7 +59,7 @@ func (api *AvailabilityZonesAPI) Get(id string) (availabilityzone *AvailabilityZ ...@@ -59,7 +59,7 @@ func (api *AvailabilityZonesAPI) Get(id string) (availabilityzone *AvailabilityZ
// Returns all availability zones on an photon instance. // Returns all availability zones on an photon instance.
func (api *AvailabilityZonesAPI) GetAll() (result *AvailabilityZones, err error) { func (api *AvailabilityZonesAPI) GetAll() (result *AvailabilityZones, err error) {
uri := api.client.Endpoint + availabilityzoneUrl uri := api.client.Endpoint + availabilityzoneUrl
res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions.AccessToken) res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -71,7 +71,7 @@ func (api *AvailabilityZonesAPI) GetAll() (result *AvailabilityZones, err error) ...@@ -71,7 +71,7 @@ func (api *AvailabilityZonesAPI) GetAll() (result *AvailabilityZones, err error)
// Deletes the availability zone with specified ID. // Deletes the availability zone with specified ID.
func (api *AvailabilityZonesAPI) Delete(id string) (task *Task, err error) { func (api *AvailabilityZonesAPI) Delete(id string) (task *Task, err error) {
res, err := api.client.restClient.Delete(api.client.Endpoint+availabilityzoneUrl+"/"+id, api.client.options.TokenOptions.AccessToken) res, err := api.client.restClient.Delete(api.client.Endpoint+availabilityzoneUrl+"/"+id, api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -87,7 +87,7 @@ func (api *AvailabilityZonesAPI) GetTasks(id string, options *TaskGetOptions) (r ...@@ -87,7 +87,7 @@ func (api *AvailabilityZonesAPI) GetTasks(id string, options *TaskGetOptions) (r
if options != nil { if options != nil {
uri += getQueryString(options) uri += getQueryString(options)
} }
res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions.AccessToken) res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
......
...@@ -36,12 +36,14 @@ type Client struct { ...@@ -36,12 +36,14 @@ type Client struct {
Hosts *HostsAPI Hosts *HostsAPI
Deployments *DeploymentsAPI Deployments *DeploymentsAPI
ResourceTickets *ResourceTicketsAPI ResourceTickets *ResourceTicketsAPI
Subnets *SubnetsAPI Networks *NetworksAPI
VirtualSubnets *VirtualSubnetsAPI VirtualSubnets *VirtualSubnetsAPI
Clusters *ClustersAPI Services *ServicesAPI
Auth *AuthAPI Auth *AuthAPI
AvailabilityZones *AvailabilityZonesAPI AvailabilityZones *AvailabilityZonesAPI
Info *InfoAPI Info *InfoAPI
Routers *RoutersAPI
Subnets *SubnetsAPI
} }
// Represents Tokens // Represents Tokens
...@@ -53,6 +55,8 @@ type TokenOptions struct { ...@@ -53,6 +55,8 @@ type TokenOptions struct {
TokenType string `json:"token_type"` TokenType string `json:"token_type"`
} }
type TokenCallback func(string)
// Options for Client // Options for Client
type ClientOptions struct { type ClientOptions struct {
// When using the Tasks.Wait APIs, defines the duration of how long // When using the Tasks.Wait APIs, defines the duration of how long
...@@ -79,6 +83,11 @@ type ClientOptions struct { ...@@ -79,6 +83,11 @@ type ClientOptions struct {
// Tokens for user authentication. Default is empty. // Tokens for user authentication. Default is empty.
TokenOptions *TokenOptions TokenOptions *TokenOptions
// A function to be called if the access token was refreshed
// The client can save the new access token for future API
// calls so that it doesn't need to be refreshed again.
UpdateAccessTokenCallback TokenCallback
} }
// Creates a new photon client with specified options. If options // Creates a new photon client with specified options. If options
...@@ -110,6 +119,7 @@ func NewClient(endpoint string, options *ClientOptions, logger *log.Logger) (c * ...@@ -110,6 +119,7 @@ func NewClient(endpoint string, options *ClientOptions, logger *log.Logger) (c *
defaultOptions.RootCAs = options.RootCAs defaultOptions.RootCAs = options.RootCAs
} }
defaultOptions.IgnoreCertificate = options.IgnoreCertificate defaultOptions.IgnoreCertificate = options.IgnoreCertificate
defaultOptions.UpdateAccessTokenCallback = options.UpdateAccessTokenCallback
} }
if logger == nil { if logger == nil {
...@@ -124,9 +134,17 @@ func NewClient(endpoint string, options *ClientOptions, logger *log.Logger) (c * ...@@ -124,9 +134,17 @@ func NewClient(endpoint string, options *ClientOptions, logger *log.Logger) (c *
endpoint = strings.TrimRight(endpoint, "/") endpoint = strings.TrimRight(endpoint, "/")
tokenCallback := func(newToken string) {
c.options.TokenOptions.AccessToken = newToken
if c.options.UpdateAccessTokenCallback != nil {
c.options.UpdateAccessTokenCallback(newToken)
}
}
restClient := &restClient{ restClient := &restClient{
httpClient: &http.Client{Transport: tr}, httpClient: &http.Client{Transport: tr},
logger: logger, logger: logger,
UpdateAccessTokenCallback: tokenCallback,
} }
c = &Client{Endpoint: endpoint, restClient: restClient, logger: logger} c = &Client{Endpoint: endpoint, restClient: restClient, logger: logger}
...@@ -145,12 +163,18 @@ func NewClient(endpoint string, options *ClientOptions, logger *log.Logger) (c * ...@@ -145,12 +163,18 @@ func NewClient(endpoint string, options *ClientOptions, logger *log.Logger) (c *
c.Hosts = &HostsAPI{c} c.Hosts = &HostsAPI{c}
c.Deployments = &DeploymentsAPI{c} c.Deployments = &DeploymentsAPI{c}
c.ResourceTickets = &ResourceTicketsAPI{c} c.ResourceTickets = &ResourceTicketsAPI{c}
c.Subnets = &SubnetsAPI{c} c.Networks = &NetworksAPI{c}
c.VirtualSubnets = &VirtualSubnetsAPI{c} c.VirtualSubnets = &VirtualSubnetsAPI{c}
c.Clusters = &ClustersAPI{c} c.Services = &ServicesAPI{c}
c.Auth = &AuthAPI{c} c.Auth = &AuthAPI{c}
c.AvailabilityZones = &AvailabilityZonesAPI{c} c.AvailabilityZones = &AvailabilityZonesAPI{c}
c.Info = &InfoAPI{c} c.Info = &InfoAPI{c}
c.Routers = &RoutersAPI{c}
c.Subnets = &SubnetsAPI{c}
// Tell the restClient about the Auth API so it can request new
// acces tokens when they expire
restClient.Auth = c.Auth
return return
} }
......
...@@ -19,7 +19,7 @@ type DeploymentsAPI struct { ...@@ -19,7 +19,7 @@ type DeploymentsAPI struct {
client *Client client *Client
} }
var deploymentUrl string = "/deployments" var deploymentUrl string = rootUrl + "/deployments"
// Creates a deployment // Creates a deployment
func (api *DeploymentsAPI) Create(deploymentSpec *DeploymentCreateSpec) (task *Task, err error) { func (api *DeploymentsAPI) Create(deploymentSpec *DeploymentCreateSpec) (task *Task, err error) {
...@@ -30,8 +30,8 @@ func (api *DeploymentsAPI) Create(deploymentSpec *DeploymentCreateSpec) (task *T ...@@ -30,8 +30,8 @@ func (api *DeploymentsAPI) Create(deploymentSpec *DeploymentCreateSpec) (task *T
res, err := api.client.restClient.Post( res, err := api.client.restClient.Post(
api.client.Endpoint+deploymentUrl, api.client.Endpoint+deploymentUrl,
"application/json", "application/json",
bytes.NewBuffer(body), bytes.NewReader(body),
api.client.options.TokenOptions.AccessToken) api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -42,7 +42,7 @@ func (api *DeploymentsAPI) Create(deploymentSpec *DeploymentCreateSpec) (task *T ...@@ -42,7 +42,7 @@ func (api *DeploymentsAPI) Create(deploymentSpec *DeploymentCreateSpec) (task *T
// Deletes a deployment with specified ID. // Deletes a deployment with specified ID.
func (api *DeploymentsAPI) Delete(id string) (task *Task, err error) { func (api *DeploymentsAPI) Delete(id string) (task *Task, err error) {
res, err := api.client.restClient.Delete(api.getEntityUrl(id), api.client.options.TokenOptions.AccessToken) res, err := api.client.restClient.Delete(api.getEntityUrl(id), api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -61,8 +61,8 @@ func (api *DeploymentsAPI) Deploy(id string, config *DeploymentDeployOperation) ...@@ -61,8 +61,8 @@ func (api *DeploymentsAPI) Deploy(id string, config *DeploymentDeployOperation)
res, err := api.client.restClient.Post( res, err := api.client.restClient.Post(
api.getEntityUrl(id)+"/deploy", api.getEntityUrl(id)+"/deploy",
"application/json", "application/json",
bytes.NewBuffer(body), bytes.NewReader(body),
api.client.options.TokenOptions.AccessToken) api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -76,8 +76,8 @@ func (api *DeploymentsAPI) Destroy(id string) (task *Task, err error) { ...@@ -76,8 +76,8 @@ func (api *DeploymentsAPI) Destroy(id string) (task *Task, err error) {
res, err := api.client.restClient.Post( res, err := api.client.restClient.Post(
api.getEntityUrl(id)+"/destroy", api.getEntityUrl(id)+"/destroy",
"application/json", "application/json",
bytes.NewBuffer([]byte("")), bytes.NewReader([]byte("")),
api.client.options.TokenOptions.AccessToken) api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -88,7 +88,7 @@ func (api *DeploymentsAPI) Destroy(id string) (task *Task, err error) { ...@@ -88,7 +88,7 @@ func (api *DeploymentsAPI) Destroy(id string) (task *Task, err error) {
// Returns all deployments. // Returns all deployments.
func (api *DeploymentsAPI) GetAll() (result *Deployments, err error) { func (api *DeploymentsAPI) GetAll() (result *Deployments, err error) {
res, err := api.client.restClient.Get(api.client.Endpoint+deploymentUrl, api.client.options.TokenOptions.AccessToken) res, err := api.client.restClient.Get(api.client.Endpoint+deploymentUrl, api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -104,7 +104,7 @@ func (api *DeploymentsAPI) GetAll() (result *Deployments, err error) { ...@@ -104,7 +104,7 @@ func (api *DeploymentsAPI) GetAll() (result *Deployments, err error) {
// Gets a deployment with the specified ID. // Gets a deployment with the specified ID.
func (api *DeploymentsAPI) Get(id string) (deployment *Deployment, err error) { func (api *DeploymentsAPI) Get(id string) (deployment *Deployment, err error) {
res, err := api.client.restClient.Get(api.getEntityUrl(id), api.client.options.TokenOptions.AccessToken) res, err := api.client.restClient.Get(api.getEntityUrl(id), api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -121,7 +121,7 @@ func (api *DeploymentsAPI) Get(id string) (deployment *Deployment, err error) { ...@@ -121,7 +121,7 @@ func (api *DeploymentsAPI) Get(id string) (deployment *Deployment, err error) {
// Gets all hosts with the specified deployment ID. // Gets all hosts with the specified deployment ID.
func (api *DeploymentsAPI) GetHosts(id string) (result *Hosts, err error) { func (api *DeploymentsAPI) GetHosts(id string) (result *Hosts, err error) {
uri := api.getEntityUrl(id) + "/hosts" uri := api.getEntityUrl(id) + "/hosts"
res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions.AccessToken) res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -134,7 +134,7 @@ func (api *DeploymentsAPI) GetHosts(id string) (result *Hosts, err error) { ...@@ -134,7 +134,7 @@ func (api *DeploymentsAPI) GetHosts(id string) (result *Hosts, err error) {
// Gets all the vms with the specified deployment ID. // Gets all the vms with the specified deployment ID.
func (api *DeploymentsAPI) GetVms(id string) (result *VMs, err error) { func (api *DeploymentsAPI) GetVms(id string) (result *VMs, err error) {
uri := api.getEntityUrl(id) + "/vms" uri := api.getEntityUrl(id) + "/vms"
res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions.AccessToken) res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -153,8 +153,8 @@ func (api *DeploymentsAPI) InitializeDeploymentMigration(sourceAddress *Initiali ...@@ -153,8 +153,8 @@ func (api *DeploymentsAPI) InitializeDeploymentMigration(sourceAddress *Initiali
res, err := api.client.restClient.Post( res, err := api.client.restClient.Post(
api.getEntityUrl(id)+"/initialize_migration", api.getEntityUrl(id)+"/initialize_migration",
"application/json", "application/json",
bytes.NewBuffer(body), bytes.NewReader(body),
api.client.options.TokenOptions.AccessToken) api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -172,8 +172,8 @@ func (api *DeploymentsAPI) FinalizeDeploymentMigration(sourceAddress *FinalizeMi ...@@ -172,8 +172,8 @@ func (api *DeploymentsAPI) FinalizeDeploymentMigration(sourceAddress *FinalizeMi
res, err := api.client.restClient.Post( res, err := api.client.restClient.Post(
api.getEntityUrl(id)+"/finalize_migration", api.getEntityUrl(id)+"/finalize_migration",
"application/json", "application/json",
bytes.NewBuffer(body), bytes.NewReader(body),
api.client.options.TokenOptions.AccessToken) api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -196,8 +196,24 @@ func (api *DeploymentsAPI) SetImageDatastores(id string, imageDatastores *ImageD ...@@ -196,8 +196,24 @@ func (api *DeploymentsAPI) SetImageDatastores(id string, imageDatastores *ImageD
res, err := api.client.restClient.Post( res, err := api.client.restClient.Post(
api.getEntityUrl(id)+"/set_image_datastores", api.getEntityUrl(id)+"/set_image_datastores",
"application/json", "application/json",
bytes.NewBuffer(body), bytes.NewReader(body),
api.client.options.TokenOptions.AccessToken) api.client.options.TokenOptions)
if err != nil {
return
}
defer res.Body.Close()
task, err = getTask(getError(res))
return
}
// Synchronizes hosts configurations
func (api *DeploymentsAPI) SyncHostsConfig(id string) (task *Task, err error) {
res, err := api.client.restClient.Post(
api.getEntityUrl(id)+"/sync_hosts_config",
"application/json",
bytes.NewReader([]byte("")),
api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -212,8 +228,8 @@ func (api *DeploymentsAPI) PauseSystem(id string) (task *Task, err error) { ...@@ -212,8 +228,8 @@ func (api *DeploymentsAPI) PauseSystem(id string) (task *Task, err error) {
res, err := api.client.restClient.Post( res, err := api.client.restClient.Post(
api.getEntityUrl(id)+"/pause_system", api.getEntityUrl(id)+"/pause_system",
"application/json", "application/json",
bytes.NewBuffer([]byte("")), bytes.NewReader([]byte("")),
api.client.options.TokenOptions.AccessToken) api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -228,8 +244,8 @@ func (api *DeploymentsAPI) PauseBackgroundTasks(id string) (task *Task, err erro ...@@ -228,8 +244,8 @@ func (api *DeploymentsAPI) PauseBackgroundTasks(id string) (task *Task, err erro
res, err := api.client.restClient.Post( res, err := api.client.restClient.Post(
api.getEntityUrl(id)+"/pause_background_tasks", api.getEntityUrl(id)+"/pause_background_tasks",
"application/json", "application/json",
bytes.NewBuffer([]byte("")), bytes.NewReader([]byte("")),
api.client.options.TokenOptions.AccessToken) api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -244,8 +260,28 @@ func (api *DeploymentsAPI) ResumeSystem(id string) (task *Task, err error) { ...@@ -244,8 +260,28 @@ func (api *DeploymentsAPI) ResumeSystem(id string) (task *Task, err error) {
res, err := api.client.restClient.Post( res, err := api.client.restClient.Post(
api.getEntityUrl(id)+"/resume_system", api.getEntityUrl(id)+"/resume_system",
"application/json", "application/json",
bytes.NewBuffer([]byte("")), bytes.NewReader([]byte("")),
api.client.options.TokenOptions.AccessToken) api.client.options.TokenOptions)
if err != nil {
return
}
defer res.Body.Close()
task, err = getTask(getError(res))
return
}
// Enable service type with specified deployment ID.
func (api *DeploymentsAPI) EnableServiceType(id string, serviceConfigSpec *ServiceConfigurationSpec) (task *Task, err error) {
body, err := json.Marshal(serviceConfigSpec)
if err != nil {
return
}
res, err := api.client.restClient.Post(
api.getEntityUrl(id)+"/enable_service_type",
"application/json",
bytes.NewReader(body),
api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -255,17 +291,17 @@ func (api *DeploymentsAPI) ResumeSystem(id string) (task *Task, err error) { ...@@ -255,17 +291,17 @@ func (api *DeploymentsAPI) ResumeSystem(id string) (task *Task, err error) {
return return
} }
// Enable cluster type with specified deployment ID. // Disable service type with specified deployment ID.
func (api *DeploymentsAPI) EnableClusterType(id string, clusterConfigSpec *ClusterConfigurationSpec) (task *Task, err error) { func (api *DeploymentsAPI) DisableServiceType(id string, serviceConfigSpec *ServiceConfigurationSpec) (task *Task, err error) {
body, err := json.Marshal(clusterConfigSpec) body, err := json.Marshal(serviceConfigSpec)
if err != nil { if err != nil {
return return
} }
res, err := api.client.restClient.Post( res, err := api.client.restClient.Post(
api.getEntityUrl(id)+"/enable_cluster_type", api.getEntityUrl(id)+"/disable_service_type",
"application/json", "application/json",
bytes.NewBuffer(body), bytes.NewReader(body),
api.client.options.TokenOptions.AccessToken) api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -275,17 +311,18 @@ func (api *DeploymentsAPI) EnableClusterType(id string, clusterConfigSpec *Clust ...@@ -275,17 +311,18 @@ func (api *DeploymentsAPI) EnableClusterType(id string, clusterConfigSpec *Clust
return return
} }
// Disable cluster type with specified deployment ID. // Configure NSX.
func (api *DeploymentsAPI) DisableClusterType(id string, clusterConfigSpec *ClusterConfigurationSpec) (task *Task, err error) { func (api *DeploymentsAPI) ConfigureNsx(id string, nsxConfigSpec *NsxConfigurationSpec) (task *Task, err error) {
body, err := json.Marshal(clusterConfigSpec) body, err := json.Marshal(nsxConfigSpec)
if err != nil { if err != nil {
return return
} }
res, err := api.client.restClient.Post( res, err := api.client.restClient.Post(
api.getEntityUrl(id)+"/disable_cluster_type", api.getEntityUrl(id)+"/configure_nsx",
"application/json", "application/json",
bytes.NewBuffer(body), bytes.NewReader(body),
api.client.options.TokenOptions.AccessToken) api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
......
...@@ -18,11 +18,11 @@ type DisksAPI struct { ...@@ -18,11 +18,11 @@ type DisksAPI struct {
client *Client client *Client
} }
var diskUrl string = "/disks/" var diskUrl string = rootUrl + "/disks/"
// Gets a PersistentDisk for the disk with specified ID. // Gets a PersistentDisk for the disk with specified ID.
func (api *DisksAPI) Get(diskID string) (disk *PersistentDisk, err error) { func (api *DisksAPI) Get(diskID string) (disk *PersistentDisk, err error) {
res, err := api.client.restClient.Get(api.client.Endpoint+diskUrl+diskID, api.client.options.TokenOptions.AccessToken) res, err := api.client.restClient.Get(api.client.Endpoint+diskUrl+diskID, api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -38,7 +38,7 @@ func (api *DisksAPI) Get(diskID string) (disk *PersistentDisk, err error) { ...@@ -38,7 +38,7 @@ func (api *DisksAPI) Get(diskID string) (disk *PersistentDisk, err error) {
// Deletes a disk with the specified ID. // Deletes a disk with the specified ID.
func (api *DisksAPI) Delete(diskID string) (task *Task, err error) { func (api *DisksAPI) Delete(diskID string) (task *Task, err error) {
res, err := api.client.restClient.Delete(api.client.Endpoint+diskUrl+diskID, api.client.options.TokenOptions.AccessToken) res, err := api.client.restClient.Delete(api.client.Endpoint+diskUrl+diskID, api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -54,7 +54,7 @@ func (api *DisksAPI) GetTasks(id string, options *TaskGetOptions) (result *TaskL ...@@ -54,7 +54,7 @@ func (api *DisksAPI) GetTasks(id string, options *TaskGetOptions) (result *TaskL
if options != nil { if options != nil {
uri += getQueryString(options) uri += getQueryString(options)
} }
res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions.AccessToken) res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
......
...@@ -25,7 +25,7 @@ type FlavorGetOptions struct { ...@@ -25,7 +25,7 @@ type FlavorGetOptions struct {
Kind string `urlParam:"kind"` Kind string `urlParam:"kind"`
} }
var flavorUrl string = "/flavors" var flavorUrl string = rootUrl + "/flavors"
// Creates a flavor. // Creates a flavor.
func (api *FlavorsAPI) Create(spec *FlavorCreateSpec) (task *Task, err error) { func (api *FlavorsAPI) Create(spec *FlavorCreateSpec) (task *Task, err error) {
...@@ -37,7 +37,7 @@ func (api *FlavorsAPI) Create(spec *FlavorCreateSpec) (task *Task, err error) { ...@@ -37,7 +37,7 @@ func (api *FlavorsAPI) Create(spec *FlavorCreateSpec) (task *Task, err error) {
api.client.Endpoint+flavorUrl, api.client.Endpoint+flavorUrl,
"application/json", "application/json",
bytes.NewReader(body), bytes.NewReader(body),
api.client.options.TokenOptions.AccessToken) api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -48,7 +48,7 @@ func (api *FlavorsAPI) Create(spec *FlavorCreateSpec) (task *Task, err error) { ...@@ -48,7 +48,7 @@ func (api *FlavorsAPI) Create(spec *FlavorCreateSpec) (task *Task, err error) {
// Gets details of flavor with specified ID. // Gets details of flavor with specified ID.
func (api *FlavorsAPI) Get(flavorID string) (flavor *Flavor, err error) { func (api *FlavorsAPI) Get(flavorID string) (flavor *Flavor, err error) {
res, err := api.client.restClient.Get(api.client.Endpoint+flavorUrl+"/"+flavorID, api.client.options.TokenOptions.AccessToken) res, err := api.client.restClient.Get(api.client.Endpoint+flavorUrl+"/"+flavorID, api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -68,7 +68,7 @@ func (api *FlavorsAPI) GetAll(options *FlavorGetOptions) (flavors *FlavorList, e ...@@ -68,7 +68,7 @@ func (api *FlavorsAPI) GetAll(options *FlavorGetOptions) (flavors *FlavorList, e
if options != nil { if options != nil {
uri += getQueryString(options) uri += getQueryString(options)
} }
res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions.AccessToken) res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -80,7 +80,7 @@ func (api *FlavorsAPI) GetAll(options *FlavorGetOptions) (flavors *FlavorList, e ...@@ -80,7 +80,7 @@ func (api *FlavorsAPI) GetAll(options *FlavorGetOptions) (flavors *FlavorList, e
// Deletes flavor with specified ID. // Deletes flavor with specified ID.
func (api *FlavorsAPI) Delete(flavorID string) (task *Task, err error) { func (api *FlavorsAPI) Delete(flavorID string) (task *Task, err error) {
res, err := api.client.restClient.Delete(api.client.Endpoint+flavorUrl+"/"+flavorID, api.client.options.TokenOptions.AccessToken) res, err := api.client.restClient.Delete(api.client.Endpoint+flavorUrl+"/"+flavorID, api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -97,7 +97,7 @@ func (api *FlavorsAPI) GetTasks(id string, options *TaskGetOptions) (result *Tas ...@@ -97,7 +97,7 @@ func (api *FlavorsAPI) GetTasks(id string, options *TaskGetOptions) (result *Tas
uri += getQueryString(options) uri += getQueryString(options)
} }
res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions.AccessToken) res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
......
...@@ -19,7 +19,7 @@ type HostsAPI struct { ...@@ -19,7 +19,7 @@ type HostsAPI struct {
client *Client client *Client
} }
var hostUrl string = "/hosts" var hostUrl string = rootUrl + "/hosts"
// Creates a host. // Creates a host.
func (api *HostsAPI) Create(hostSpec *HostCreateSpec, deploymentId string) (task *Task, err error) { func (api *HostsAPI) Create(hostSpec *HostCreateSpec, deploymentId string) (task *Task, err error) {
...@@ -28,10 +28,10 @@ func (api *HostsAPI) Create(hostSpec *HostCreateSpec, deploymentId string) (task ...@@ -28,10 +28,10 @@ func (api *HostsAPI) Create(hostSpec *HostCreateSpec, deploymentId string) (task
return return
} }
res, err := api.client.restClient.Post( res, err := api.client.restClient.Post(
api.client.Endpoint+deploymentUrl+"/"+deploymentId+hostUrl, api.client.Endpoint+deploymentUrl+"/"+deploymentId+"/hosts",
"application/json", "application/json",
bytes.NewBuffer(body), bytes.NewReader(body),
api.client.options.TokenOptions.AccessToken) api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -42,7 +42,7 @@ func (api *HostsAPI) Create(hostSpec *HostCreateSpec, deploymentId string) (task ...@@ -42,7 +42,7 @@ func (api *HostsAPI) Create(hostSpec *HostCreateSpec, deploymentId string) (task
// Deletes a host with specified ID. // Deletes a host with specified ID.
func (api *HostsAPI) Delete(id string) (task *Task, err error) { func (api *HostsAPI) Delete(id string) (task *Task, err error) {
res, err := api.client.restClient.Delete(api.client.Endpoint+hostUrl+"/"+id, api.client.options.TokenOptions.AccessToken) res, err := api.client.restClient.Delete(api.client.Endpoint+hostUrl+"/"+id, api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -53,7 +53,7 @@ func (api *HostsAPI) Delete(id string) (task *Task, err error) { ...@@ -53,7 +53,7 @@ func (api *HostsAPI) Delete(id string) (task *Task, err error) {
// Returns all hosts // Returns all hosts
func (api *HostsAPI) GetAll() (result *Hosts, err error) { func (api *HostsAPI) GetAll() (result *Hosts, err error) {
res, err := api.client.restClient.Get(api.client.Endpoint+hostUrl, api.client.options.TokenOptions.AccessToken) res, err := api.client.restClient.Get(api.client.Endpoint+hostUrl, api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -69,7 +69,7 @@ func (api *HostsAPI) GetAll() (result *Hosts, err error) { ...@@ -69,7 +69,7 @@ func (api *HostsAPI) GetAll() (result *Hosts, err error) {
// Gets a host with the specified ID. // Gets a host with the specified ID.
func (api *HostsAPI) Get(id string) (host *Host, err error) { func (api *HostsAPI) Get(id string) (host *Host, err error) {
res, err := api.client.restClient.Get(api.client.Endpoint+hostUrl+"/"+id, api.client.options.TokenOptions.AccessToken) res, err := api.client.restClient.Get(api.client.Endpoint+hostUrl+"/"+id, api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -93,8 +93,8 @@ func (api *HostsAPI) SetAvailabilityZone(id string, availabilityZone *HostSetAva ...@@ -93,8 +93,8 @@ func (api *HostsAPI) SetAvailabilityZone(id string, availabilityZone *HostSetAva
res, err := api.client.restClient.Post( res, err := api.client.restClient.Post(
api.client.Endpoint+hostUrl+"/"+id+"/set_availability_zone", api.client.Endpoint+hostUrl+"/"+id+"/set_availability_zone",
"application/json", "application/json",
bytes.NewBuffer(body), bytes.NewReader(body),
api.client.options.TokenOptions.AccessToken) api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
...@@ -112,7 +112,7 @@ func (api *HostsAPI) GetTasks(id string, options *TaskGetOptions) (result *TaskL ...@@ -112,7 +112,7 @@ func (api *HostsAPI) GetTasks(id string, options *TaskGetOptions) (result *TaskL
if options != nil { if options != nil {
uri += getQueryString(options) uri += getQueryString(options)
} }
res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions.AccessToken) res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -122,9 +122,9 @@ func (api *HostsAPI) GetTasks(id string, options *TaskGetOptions) (result *TaskL ...@@ -122,9 +122,9 @@ func (api *HostsAPI) GetTasks(id string, options *TaskGetOptions) (result *TaskL
return return
} }
// Gets all the vms with the specified deployment ID. // Gets all the vms with the specified host ID.
func (api *HostsAPI) GetVMs(id string) (result *VMs, err error) { func (api *HostsAPI) GetVMs(id string) (result *VMs, err error) {
res, err := api.client.restClient.Get(api.client.Endpoint+hostUrl+"/"+id+"/vms", api.client.options.TokenOptions.AccessToken) res, err := api.client.restClient.Get(api.client.Endpoint+hostUrl+"/"+id+"/vms", api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -138,6 +138,22 @@ func (api *HostsAPI) GetVMs(id string) (result *VMs, err error) { ...@@ -138,6 +138,22 @@ func (api *HostsAPI) GetVMs(id string) (result *VMs, err error) {
return return
} }
// provision the host with the specified id
func (api *HostsAPI) Provision(id string) (task *Task, err error) {
body := []byte{}
res, err := api.client.restClient.Post(
api.client.Endpoint+hostUrl+"/"+id+"/provision",
"application/json",
bytes.NewReader(body),
api.client.options.TokenOptions)
if err != nil {
return
}
defer res.Body.Close()
task, err = getTask(getError(res))
return
}
// Suspend the host with the specified id // Suspend the host with the specified id
func (api *HostsAPI) Suspend(id string) (task *Task, err error) { func (api *HostsAPI) Suspend(id string) (task *Task, err error) {
body := []byte{} body := []byte{}
...@@ -145,7 +161,7 @@ func (api *HostsAPI) Suspend(id string) (task *Task, err error) { ...@@ -145,7 +161,7 @@ func (api *HostsAPI) Suspend(id string) (task *Task, err error) {
api.client.Endpoint+hostUrl+"/"+id+"/suspend", api.client.Endpoint+hostUrl+"/"+id+"/suspend",
"application/json", "application/json",
bytes.NewReader(body), bytes.NewReader(body),
api.client.options.TokenOptions.AccessToken) api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -161,7 +177,7 @@ func (api *HostsAPI) Resume(id string) (task *Task, err error) { ...@@ -161,7 +177,7 @@ func (api *HostsAPI) Resume(id string) (task *Task, err error) {
api.client.Endpoint+hostUrl+"/"+id+"/resume", api.client.Endpoint+hostUrl+"/"+id+"/resume",
"application/json", "application/json",
bytes.NewReader(body), bytes.NewReader(body),
api.client.options.TokenOptions.AccessToken) api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -177,7 +193,7 @@ func (api *HostsAPI) EnterMaintenanceMode(id string) (task *Task, err error) { ...@@ -177,7 +193,7 @@ func (api *HostsAPI) EnterMaintenanceMode(id string) (task *Task, err error) {
api.client.Endpoint+hostUrl+"/"+id+"/enter_maintenance", api.client.Endpoint+hostUrl+"/"+id+"/enter_maintenance",
"application/json", "application/json",
bytes.NewReader(body), bytes.NewReader(body),
api.client.options.TokenOptions.AccessToken) api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -193,7 +209,7 @@ func (api *HostsAPI) ExitMaintenanceMode(id string) (task *Task, err error) { ...@@ -193,7 +209,7 @@ func (api *HostsAPI) ExitMaintenanceMode(id string) (task *Task, err error) {
api.client.Endpoint+hostUrl+"/"+id+"/exit_maintenance", api.client.Endpoint+hostUrl+"/"+id+"/exit_maintenance",
"application/json", "application/json",
bytes.NewReader(body), bytes.NewReader(body),
api.client.options.TokenOptions.AccessToken) api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
package photon package photon
import ( import (
"bytes"
"encoding/json" "encoding/json"
"io" "io"
) )
...@@ -24,13 +25,13 @@ type ImageGetOptions struct { ...@@ -24,13 +25,13 @@ type ImageGetOptions struct {
Name string `urlParam:"name"` Name string `urlParam:"name"`
} }
var imageUrl string = "/images" var imageUrl string = rootUrl + "/images"
// Uploads a new image, reading from the specified image path. // Uploads a new image, reading from the specified image path.
// If options is nil, default options are used. // If options is nil, default options are used.
func (api *ImagesAPI) CreateFromFile(imagePath string, options *ImageCreateOptions) (task *Task, err error) { func (api *ImagesAPI) CreateFromFile(imagePath string, options *ImageCreateOptions) (task *Task, err error) {
params := imageCreateOptionsToMap(options) params := imageCreateOptionsToMap(options)
res, err := api.client.restClient.MultipartUploadFile(api.client.Endpoint+imageUrl, imagePath, params, api.client.options.TokenOptions.AccessToken) res, err := api.client.restClient.MultipartUploadFile(api.client.Endpoint+imageUrl, imagePath, params, api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -43,9 +44,9 @@ func (api *ImagesAPI) CreateFromFile(imagePath string, options *ImageCreateOptio ...@@ -43,9 +44,9 @@ func (api *ImagesAPI) CreateFromFile(imagePath string, options *ImageCreateOptio
// Name is a descriptive name of the image, it is used in the filename field of the Content-Disposition header, // Name is a descriptive name of the image, it is used in the filename field of the Content-Disposition header,
// and does not need to be unique. // and does not need to be unique.
// If options is nil, default options are used. // If options is nil, default options are used.
func (api *ImagesAPI) Create(reader io.Reader, name string, options *ImageCreateOptions) (task *Task, err error) { func (api *ImagesAPI) Create(reader io.ReadSeeker, name string, options *ImageCreateOptions) (task *Task, err error) {
params := imageCreateOptionsToMap(options) params := imageCreateOptionsToMap(options)
res, err := api.client.restClient.MultipartUpload(api.client.Endpoint+imageUrl, reader, name, params, api.client.options.TokenOptions.AccessToken) res, err := api.client.restClient.MultipartUpload(api.client.Endpoint+imageUrl, reader, name, params, api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -60,7 +61,7 @@ func (api *ImagesAPI) GetAll(options *ImageGetOptions) (images *Images, err erro ...@@ -60,7 +61,7 @@ func (api *ImagesAPI) GetAll(options *ImageGetOptions) (images *Images, err erro
if options != nil { if options != nil {
uri += getQueryString(options) uri += getQueryString(options)
} }
res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions.AccessToken) res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -72,7 +73,7 @@ func (api *ImagesAPI) GetAll(options *ImageGetOptions) (images *Images, err erro ...@@ -72,7 +73,7 @@ func (api *ImagesAPI) GetAll(options *ImageGetOptions) (images *Images, err erro
// Gets details of image with the specified ID. // Gets details of image with the specified ID.
func (api *ImagesAPI) Get(imageID string) (image *Image, err error) { func (api *ImagesAPI) Get(imageID string) (image *Image, err error) {
res, err := api.client.restClient.Get(api.client.Endpoint+imageUrl+"/"+imageID, api.client.options.TokenOptions.AccessToken) res, err := api.client.restClient.Get(api.client.Endpoint+imageUrl+"/"+imageID, api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -88,7 +89,7 @@ func (api *ImagesAPI) Get(imageID string) (image *Image, err error) { ...@@ -88,7 +89,7 @@ func (api *ImagesAPI) Get(imageID string) (image *Image, err error) {
// Deletes image with the specified ID. // Deletes image with the specified ID.
func (api *ImagesAPI) Delete(imageID string) (task *Task, err error) { func (api *ImagesAPI) Delete(imageID string) (task *Task, err error) {
res, err := api.client.restClient.Delete(api.client.Endpoint+imageUrl+"/"+imageID, api.client.options.TokenOptions.AccessToken) res, err := api.client.restClient.Delete(api.client.Endpoint+imageUrl+"/"+imageID, api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -105,7 +106,7 @@ func (api *ImagesAPI) GetTasks(id string, options *TaskGetOptions) (result *Task ...@@ -105,7 +106,7 @@ func (api *ImagesAPI) GetTasks(id string, options *TaskGetOptions) (result *Task
uri += getQueryString(options) uri += getQueryString(options)
} }
res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions.AccessToken) res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -115,6 +116,62 @@ func (api *ImagesAPI) GetTasks(id string, options *TaskGetOptions) (result *Task ...@@ -115,6 +116,62 @@ func (api *ImagesAPI) GetTasks(id string, options *TaskGetOptions) (result *Task
return return
} }
// Gets IAM Policy of an image.
func (api *ImagesAPI) GetIam(imageID string) (policy *[]PolicyEntry, err error) {
res, err := api.client.restClient.Get(
api.client.Endpoint+imageUrl+"/"+imageID+"/iam",
api.client.options.TokenOptions)
if err != nil {
return
}
defer res.Body.Close()
res, err = getError(res)
if err != nil {
return
}
var result []PolicyEntry
err = json.NewDecoder(res.Body).Decode(&result)
return &result, nil
}
// Sets IAM Policy on an image.
func (api *ImagesAPI) SetIam(imageID string, policy *[]PolicyEntry) (task *Task, err error) {
body, err := json.Marshal(policy)
if err != nil {
return
}
res, err := api.client.restClient.Post(
api.client.Endpoint+imageUrl+"/"+imageID+"/iam",
"application/json",
bytes.NewReader(body),
api.client.options.TokenOptions)
if err != nil {
return
}
defer res.Body.Close()
task, err = getTask(getError(res))
return
}
// Modifies IAM Policy on an image.
func (api *ImagesAPI) ModifyIam(imageID string, policyDelta *PolicyDelta) (task *Task, err error) {
body, err := json.Marshal(policyDelta)
if err != nil {
return
}
res, err := api.client.restClient.Put(
api.client.Endpoint+imageUrl+"/"+imageID+"/iam",
"application/json",
bytes.NewReader(body),
api.client.options.TokenOptions)
if err != nil {
return
}
defer res.Body.Close()
task, err = getTask(getError(res))
return
}
func imageCreateOptionsToMap(opts *ImageCreateOptions) map[string]string { func imageCreateOptionsToMap(opts *ImageCreateOptions) map[string]string {
if opts == nil { if opts == nil {
return nil return nil
......
...@@ -17,11 +17,11 @@ type InfoAPI struct { ...@@ -17,11 +17,11 @@ type InfoAPI struct {
client *Client client *Client
} }
var infoUrl = "/info" var infoUrl = rootUrl + "/info"
// Get info // Get info
func (api *InfoAPI) Get() (info *Info, err error) { func (api *InfoAPI) Get() (info *Info, err error) {
res, err := api.client.restClient.Get(api.client.Endpoint+infoUrl, api.client.options.TokenOptions.AccessToken) res, err := api.client.restClient.Get(api.client.Endpoint+infoUrl, api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
......
...@@ -18,6 +18,7 @@ import ( ...@@ -18,6 +18,7 @@ import (
"io/ioutil" "io/ioutil"
"log" "log"
"net/http" "net/http"
"net/url"
"strings" "strings"
) )
...@@ -168,6 +169,8 @@ type OIDCTokenResponse struct { ...@@ -168,6 +169,8 @@ type OIDCTokenResponse struct {
} }
func (client *OIDCClient) GetTokenByPasswordGrant(username string, password string) (tokens *OIDCTokenResponse, err error) { func (client *OIDCClient) GetTokenByPasswordGrant(username string, password string) (tokens *OIDCTokenResponse, err error) {
username = url.QueryEscape(username)
password = url.QueryEscape(password)
body := fmt.Sprintf(passwordGrantFormatString, username, password, client.Options.TokenScope) body := fmt.Sprintf(passwordGrantFormatString, username, password, client.Options.TokenScope)
return client.getToken(body) return client.getToken(body)
} }
......
// Copyright (c) 2017 VMware, Inc. All Rights Reserved.
//
// This product is licensed to you under the Apache License, Version 2.0 (the "License").
// You may not use this product except in compliance with the License.
//
// This product may include a number of subcomponents with separate copyright notices and
// license terms. Your use of these subcomponents is subject to the terms and conditions
// of the subcomponent's license, as noted in the LICENSE file.
// +build windows
package lightwave
import (
"encoding/base64"
"fmt"
"github.com/vmware/photon-controller-go-sdk/SSPI"
"math/rand"
"net"
"net/url"
"strings"
"time"
)
const gssTicketGrantFormatString = "grant_type=urn:vmware:grant_type:gss_ticket&gss_ticket=%s&context_id=%s&scope=%s"
// GetTokensFromWindowsLogInContext gets tokens based on Windows logged in context
// Here is how it works:
// 1. Get the SPN (Service Principal Name) in the format host/FQDN of lightwave. This is needed for SSPI/Kerberos protocol
// 2. Call Windows API AcquireCredentialsHandle() using SSPI library. This will give the current users credential handle
// 3. Using this handle call Windows API AcquireCredentialsHandle(). This will give you byte[]
// 4. Encode this byte[] and send it to OIDC server over HTTP (using POST)
// 5. OIDC server can send either of the following
// - Access tokens. In this case return access tokens to client
// - Error in the format: invalid_grant: gss_continue_needed:'context id':'token from server'
// 6. In case you get error, parse it and get the token from server
// 7. Feed this token to step 3 and repeat steps till you get the access tokens from server
func (client *OIDCClient) GetTokensFromWindowsLogInContext() (tokens *OIDCTokenResponse, err error) {
spn, err := client.buildSPN()
if err != nil {
return nil, err
}
auth, _ := SSPI.GetAuth("", "", spn, "")
userContext, err := auth.InitialBytes()
if err != nil {
return nil, err
}
// In case of multiple req/res between client and server (as explained in above comment),
// server needs to maintain the mapping of context id -> token
// So we need to generate random string as a context id
// If we use same context id for all the requests, results can be erroneous
contextId := client.generateRandomString()
body := fmt.Sprintf(gssTicketGrantFormatString, url.QueryEscape(base64.StdEncoding.EncodeToString(userContext)), contextId, client.Options.TokenScope)
tokens, err = client.getToken(body)
for {
if err == nil {
break
}
// In case of error the response will be in format: invalid_grant: gss_continue_needed:'context id':'token from server'
gssToken := client.validateAndExtractGSSResponse(err, contextId)
if gssToken == "" {
return nil, err
}
data, err := base64.StdEncoding.DecodeString(gssToken)
if err != nil {
return nil, err
}
userContext, err := auth.NextBytes(data)
body := fmt.Sprintf(gssTicketGrantFormatString, url.QueryEscape(base64.StdEncoding.EncodeToString(userContext)), contextId, client.Options.TokenScope)
tokens, err = client.getToken(body)
}
return tokens, err
}
// Gets the SPN (Service Principal Name) in the format host/FQDN of lightwave
func (client *OIDCClient) buildSPN() (spn string, err error) {
u, err := url.Parse(client.Endpoint)
if err != nil {
return "", err
}
host, _, err := net.SplitHostPort(u.Host)
if err != nil {
return "", err
}
addr, err := net.LookupAddr(host)
if err != nil {
return "", err
}
var s = strings.TrimSuffix(addr[0], ".")
return "host/" + s, nil
}
// validateAndExtractGSSResponse parse the error from server and returns token from server
// In case of error from the server, response will be in format: invalid_grant: gss_continue_needed:'context id':'token from server'
// So, we check for the above format in error and then return the token from server
// If error is not in above format, we return empty string
func (client *OIDCClient) validateAndExtractGSSResponse(err error, contextId string) string {
parts := strings.Split(err.Error(), ":")
if !(len(parts) == 4 && strings.TrimSpace(parts[1]) == "gss_continue_needed" && parts[2] == contextId) {
return ""
} else {
return parts[3]
}
}
func (client *OIDCClient) generateRandomString() string {
const length = 10
const asciiA = 65
const asciiZ = 90
rand.Seed(time.Now().UTC().UnixNano())
bytes := make([]byte, length)
for i := 0; i < length; i++ {
bytes[i] = byte(randInt(asciiA, asciiZ))
}
return string(bytes)
}
func randInt(min int, max int) int {
return min + rand.Intn(max-min)
}
// Copyright (c) 2017 VMware, Inc. All Rights Reserved.
//
// This product is licensed to you under the Apache License, Version 2.0 (the "License").
// You may not use this product except in compliance with the License.
//
// This product may include a number of subcomponents with separate copyright notices and
// license terms. Your use of these subcomponents is subject to the terms and conditions
// of the subcomponent's license, as noted in the LICENSE file.
// +build !windows
package lightwave
import "errors"
func (client *OIDCClient) GetTokensFromWindowsLogInContext() (tokens *OIDCTokenResponse, err error) {
return nil, errors.New("Not supported on this OS")
}
...@@ -15,28 +15,28 @@ import ( ...@@ -15,28 +15,28 @@ import (
) )
// Contains functionality for networks API. // Contains functionality for networks API.
type SubnetsAPI struct { type NetworksAPI struct {
client *Client client *Client
} }
// Options used for GetAll API // Options used for GetAll API
type SubnetGetOptions struct { type NetworkGetOptions struct {
Name string `urlParam:"name"` Name string `urlParam:"name"`
} }
var subnetUrl string = "/subnets" var networkUrl string = rootUrl + "/subnets"
// Creates a network. // Creates a network.
func (api *SubnetsAPI) Create(networkSpec *SubnetCreateSpec) (task *Task, err error) { func (api *NetworksAPI) Create(networkSpec *NetworkCreateSpec) (task *Task, err error) {
body, err := json.Marshal(networkSpec) body, err := json.Marshal(networkSpec)
if err != nil { if err != nil {
return return
} }
res, err := api.client.restClient.Post( res, err := api.client.restClient.Post(
api.client.Endpoint+subnetUrl, api.client.Endpoint+networkUrl,
"application/json", "application/json",
bytes.NewBuffer(body), bytes.NewReader(body),
api.client.options.TokenOptions.AccessToken) api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -46,8 +46,8 @@ func (api *SubnetsAPI) Create(networkSpec *SubnetCreateSpec) (task *Task, err er ...@@ -46,8 +46,8 @@ func (api *SubnetsAPI) Create(networkSpec *SubnetCreateSpec) (task *Task, err er
} }
// Deletes a network with specified ID. // Deletes a network with specified ID.
func (api *SubnetsAPI) Delete(id string) (task *Task, err error) { func (api *NetworksAPI) Delete(id string) (task *Task, err error) {
res, err := api.client.restClient.Delete(api.client.Endpoint+subnetUrl+"/"+id, api.client.options.TokenOptions.AccessToken) res, err := api.client.restClient.Delete(api.client.Endpoint+networkUrl+"/"+id, api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -57,8 +57,8 @@ func (api *SubnetsAPI) Delete(id string) (task *Task, err error) { ...@@ -57,8 +57,8 @@ func (api *SubnetsAPI) Delete(id string) (task *Task, err error) {
} }
// Gets a network with the specified ID. // Gets a network with the specified ID.
func (api *SubnetsAPI) Get(id string) (network *Subnet, err error) { func (api *NetworksAPI) Get(id string) (network *Network, err error) {
res, err := api.client.restClient.Get(api.client.Endpoint+subnetUrl+"/"+id, api.client.options.TokenOptions.AccessToken) res, err := api.client.restClient.Get(api.client.Endpoint+networkUrl+"/"+id, api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -67,31 +67,31 @@ func (api *SubnetsAPI) Get(id string) (network *Subnet, err error) { ...@@ -67,31 +67,31 @@ func (api *SubnetsAPI) Get(id string) (network *Subnet, err error) {
if err != nil { if err != nil {
return return
} }
var result Subnet var result Network
err = json.NewDecoder(res.Body).Decode(&result) err = json.NewDecoder(res.Body).Decode(&result)
return &result, nil return &result, nil
} }
// Returns all networks // Returns all networks
func (api *SubnetsAPI) GetAll(options *SubnetGetOptions) (result *Subnets, err error) { func (api *NetworksAPI) GetAll(options *NetworkGetOptions) (result *Networks, err error) {
uri := api.client.Endpoint + subnetUrl uri := api.client.Endpoint + networkUrl
if options != nil { if options != nil {
uri += getQueryString(options) uri += getQueryString(options)
} }
res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions.AccessToken) res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions)
result = &Subnets{} result = &Networks{}
err = json.Unmarshal(res, result) err = json.Unmarshal(res, result)
return return
} }
// Sets default network. // Sets default network.
func (api *SubnetsAPI) SetDefault(id string) (task *Task, err error) { func (api *NetworksAPI) SetDefault(id string) (task *Task, err error) {
res, err := api.client.restClient.Post( res, err := api.client.restClient.Post(
api.client.Endpoint+subnetUrl+"/"+id+"/set_default", api.client.Endpoint+networkUrl+"/"+id+"/set_default",
"application/json", "application/json",
bytes.NewBuffer([]byte("")), bytes.NewReader([]byte("")),
api.client.options.TokenOptions.AccessToken) api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
......
...@@ -29,11 +29,11 @@ type VmGetOptions struct { ...@@ -29,11 +29,11 @@ type VmGetOptions struct {
Name string `urlParam:"name"` Name string `urlParam:"name"`
} }
var projectUrl string = "/projects/" var projectUrl string = rootUrl + "/projects/"
// Deletes the project with specified ID. Any VMs, disks, etc., owned by the project must be deleted first. // Deletes the project with specified ID. Any VMs, disks, etc., owned by the project must be deleted first.
func (api *ProjectsAPI) Delete(projectID string) (task *Task, err error) { func (api *ProjectsAPI) Delete(projectID string) (task *Task, err error) {
res, err := api.client.restClient.Delete(api.client.Endpoint+projectUrl+projectID, api.client.options.TokenOptions.AccessToken) res, err := api.client.restClient.Delete(api.client.Endpoint+projectUrl+projectID, api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -52,7 +52,7 @@ func (api *ProjectsAPI) CreateDisk(projectID string, spec *DiskCreateSpec) (task ...@@ -52,7 +52,7 @@ func (api *ProjectsAPI) CreateDisk(projectID string, spec *DiskCreateSpec) (task
api.client.Endpoint+projectUrl+projectID+"/disks", api.client.Endpoint+projectUrl+projectID+"/disks",
"application/json", "application/json",
bytes.NewReader(body), bytes.NewReader(body),
api.client.options.TokenOptions.AccessToken) api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -68,7 +68,7 @@ func (api *ProjectsAPI) GetDisks(projectID string, options *DiskGetOptions) (res ...@@ -68,7 +68,7 @@ func (api *ProjectsAPI) GetDisks(projectID string, options *DiskGetOptions) (res
if options != nil { if options != nil {
uri += getQueryString(options) uri += getQueryString(options)
} }
res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions.AccessToken) res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -88,7 +88,7 @@ func (api *ProjectsAPI) CreateVM(projectID string, spec *VmCreateSpec) (task *Ta ...@@ -88,7 +88,7 @@ func (api *ProjectsAPI) CreateVM(projectID string, spec *VmCreateSpec) (task *Ta
api.client.Endpoint+projectUrl+projectID+"/vms", api.client.Endpoint+projectUrl+projectID+"/vms",
"application/json", "application/json",
bytes.NewReader(body), bytes.NewReader(body),
api.client.options.TokenOptions.AccessToken) api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -104,7 +104,7 @@ func (api *ProjectsAPI) GetTasks(id string, options *TaskGetOptions) (result *Ta ...@@ -104,7 +104,7 @@ func (api *ProjectsAPI) GetTasks(id string, options *TaskGetOptions) (result *Ta
if options != nil { if options != nil {
uri += getQueryString(options) uri += getQueryString(options)
} }
res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions.AccessToken) res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -121,7 +121,7 @@ func (api *ProjectsAPI) GetVMs(projectID string, options *VmGetOptions) (result ...@@ -121,7 +121,7 @@ func (api *ProjectsAPI) GetVMs(projectID string, options *VmGetOptions) (result
if options != nil { if options != nil {
uri += getQueryString(options) uri += getQueryString(options)
} }
res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions.AccessToken) res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -131,17 +131,17 @@ func (api *ProjectsAPI) GetVMs(projectID string, options *VmGetOptions) (result ...@@ -131,17 +131,17 @@ func (api *ProjectsAPI) GetVMs(projectID string, options *VmGetOptions) (result
return return
} }
// Creates a cluster on the specified project. // Creates a service on the specified project.
func (api *ProjectsAPI) CreateCluster(projectID string, spec *ClusterCreateSpec) (task *Task, err error) { func (api *ProjectsAPI) CreateService(projectID string, spec *ServiceCreateSpec) (task *Task, err error) {
body, err := json.Marshal(spec) body, err := json.Marshal(spec)
if err != nil { if err != nil {
return return
} }
res, err := api.client.restClient.Post( res, err := api.client.restClient.Post(
api.client.Endpoint+projectUrl+projectID+"/clusters", api.client.Endpoint+projectUrl+projectID+"/services",
"application/json", "application/json",
bytes.NewReader(body), bytes.NewReader(body),
api.client.options.TokenOptions.AccessToken) api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -150,22 +150,22 @@ func (api *ProjectsAPI) CreateCluster(projectID string, spec *ClusterCreateSpec) ...@@ -150,22 +150,22 @@ func (api *ProjectsAPI) CreateCluster(projectID string, spec *ClusterCreateSpec)
return return
} }
// Gets clusters for project with the specified ID // Gets services for project with the specified ID
func (api *ProjectsAPI) GetClusters(projectID string) (result *Clusters, err error) { func (api *ProjectsAPI) GetServices(projectID string) (result *Services, err error) {
uri := api.client.Endpoint + projectUrl + projectID + "/clusters" uri := api.client.Endpoint + projectUrl + projectID + "/services"
res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions.AccessToken) res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
result = &Clusters{} result = &Services{}
err = json.Unmarshal(res, result) err = json.Unmarshal(res, result)
return return
} }
// Gets the project with a specified ID. // Gets the project with a specified ID.
func (api *ProjectsAPI) Get(id string) (project *ProjectCompact, err error) { func (api *ProjectsAPI) Get(id string) (project *ProjectCompact, err error) {
res, err := api.client.restClient.Get(api.getEntityUrl(id), api.client.options.TokenOptions.AccessToken) res, err := api.client.restClient.Get(api.getEntityUrl(id), api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -187,3 +187,22 @@ func (api *ProjectsAPI) SetSecurityGroups(projectID string, securityGroups *Secu ...@@ -187,3 +187,22 @@ func (api *ProjectsAPI) SetSecurityGroups(projectID string, securityGroups *Secu
func (api *ProjectsAPI) getEntityUrl(id string) string { func (api *ProjectsAPI) getEntityUrl(id string) string {
return api.client.Endpoint + projectUrl + id return api.client.Endpoint + projectUrl + id
} }
// Creates a router on the specified project.
func (api *ProjectsAPI) CreateRouter(projectID string, spec *RouterCreateSpec) (task *Task, err error) {
body, err := json.Marshal(spec)
if err != nil {
return
}
res, err := api.client.restClient.Post(
api.client.Endpoint+projectUrl+projectID+"/routers",
"application/json",
bytes.NewReader(body),
api.client.options.TokenOptions)
if err != nil {
return
}
defer res.Body.Close()
task, err = getTask(getError(res))
return
}
...@@ -18,7 +18,7 @@ type ResourceTicketsAPI struct { ...@@ -18,7 +18,7 @@ type ResourceTicketsAPI struct {
client *Client client *Client
} }
var resourceTicketUrl string = "/resource-tickets/" var resourceTicketUrl string = rootUrl + "/resource-tickets/"
// Gets all tasks with the specified resource ticket ID, using options to filter the results. // Gets all tasks with the specified resource ticket ID, using options to filter the results.
// If options is nil, no filtering will occur. // If options is nil, no filtering will occur.
...@@ -27,7 +27,7 @@ func (api *ResourceTicketsAPI) GetTasks(id string, options *TaskGetOptions) (res ...@@ -27,7 +27,7 @@ func (api *ResourceTicketsAPI) GetTasks(id string, options *TaskGetOptions) (res
if options != nil { if options != nil {
uri += getQueryString(options) uri += getQueryString(options)
} }
res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions.AccessToken) res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -40,7 +40,7 @@ func (api *ResourceTicketsAPI) GetTasks(id string, options *TaskGetOptions) (res ...@@ -40,7 +40,7 @@ func (api *ResourceTicketsAPI) GetTasks(id string, options *TaskGetOptions) (res
// Gets the resource ticket with a specified ID. // Gets the resource ticket with a specified ID.
func (api *ResourceTicketsAPI) Get(id string) (resourceTicket *ResourceTicket, err error) { func (api *ResourceTicketsAPI) Get(id string) (resourceTicket *ResourceTicket, err error) {
res, err := api.client.restClient.Get(api.client.Endpoint+resourceTicketUrl+"/"+id, res, err := api.client.restClient.Get(api.client.Endpoint+resourceTicketUrl+"/"+id,
api.client.options.TokenOptions.AccessToken) api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
......
...@@ -10,10 +10,12 @@ ...@@ -10,10 +10,12 @@
package photon package photon
import ( import (
"bytes"
"crypto/rand" "crypto/rand"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"log" "log"
"net/http" "net/http"
"os" "os"
...@@ -22,8 +24,10 @@ import ( ...@@ -22,8 +24,10 @@ import (
) )
type restClient struct { type restClient struct {
httpClient *http.Client httpClient *http.Client
logger *log.Logger logger *log.Logger
Auth *AuthAPI
UpdateAccessTokenCallback TokenCallback
} }
type request struct { type request struct {
...@@ -31,7 +35,7 @@ type request struct { ...@@ -31,7 +35,7 @@ type request struct {
URL string URL string
ContentType string ContentType string
Body io.Reader Body io.Reader
Token string Tokens *TokenOptions
} }
type page struct { type page struct {
...@@ -44,8 +48,13 @@ type documentList struct { ...@@ -44,8 +48,13 @@ type documentList struct {
Items []interface{} Items []interface{}
} }
type bodyRewinder func() io.Reader
const appJson string = "application/json" const appJson string = "application/json"
// Root URL specifies the API version.
const rootUrl string = "/v1"
// From https://golang.org/src/mime/multipart/writer.go // From https://golang.org/src/mime/multipart/writer.go
var quoteEscaper = strings.NewReplacer("\\", "\\\\", `"`, "\\\"") var quoteEscaper = strings.NewReplacer("\\", "\\\\", `"`, "\\\"")
...@@ -65,15 +74,15 @@ func (client *restClient) AppendSlice(origSlice []interface{}, dataToAppend []in ...@@ -65,15 +74,15 @@ func (client *restClient) AppendSlice(origSlice []interface{}, dataToAppend []in
return origSlice return origSlice
} }
func (client *restClient) Get(url string, token string) (res *http.Response, err error) { func (client *restClient) Get(url string, tokens *TokenOptions) (res *http.Response, err error) {
req := request{"GET", url, "", nil, token} req := request{"GET", url, "", nil, tokens}
res, err = client.Do(&req) res, err = client.SendRequest(&req, nil)
return return
} }
func (client *restClient) GetList(endpoint string, url string, token string) (result []byte, err error) { func (client *restClient) GetList(endpoint string, url string, tokens *TokenOptions) (result []byte, err error) {
req := request{"GET", url, "", nil, token} req := request{"GET", url, "", nil, tokens}
res, err := client.Do(&req) res, err := client.SendRequest(&req, nil)
if err != nil { if err != nil {
return return
} }
...@@ -95,8 +104,8 @@ func (client *restClient) GetList(endpoint string, url string, token string) (re ...@@ -95,8 +104,8 @@ func (client *restClient) GetList(endpoint string, url string, token string) (re
documentList.Items = client.AppendSlice(documentList.Items, page.Items) documentList.Items = client.AppendSlice(documentList.Items, page.Items)
for page.NextPageLink != "" { for page.NextPageLink != "" {
req = request{"GET", endpoint + page.NextPageLink, "", nil, token} req = request{"GET", endpoint + page.NextPageLink, "", nil, tokens}
res, err = client.Do(&req) res, err = client.SendRequest(&req, nil)
if err != nil { if err != nil {
return return
} }
...@@ -124,23 +133,111 @@ func (client *restClient) GetList(endpoint string, url string, token string) (re ...@@ -124,23 +133,111 @@ func (client *restClient) GetList(endpoint string, url string, token string) (re
return return
} }
func (client *restClient) Post(url string, contentType string, body io.Reader, token string) (res *http.Response, err error) { func (client *restClient) Post(url string, contentType string, body io.ReadSeeker, tokens *TokenOptions) (res *http.Response, err error) {
if contentType == "" { if contentType == "" {
contentType = appJson contentType = appJson
} }
req := request{"POST", url, contentType, body, token} req := request{"POST", url, contentType, body, tokens}
res, err = client.Do(&req) rewinder := func() io.Reader {
body.Seek(0, 0)
return body
}
res, err = client.SendRequest(&req, rewinder)
return return
} }
func (client *restClient) Delete(url string, token string) (res *http.Response, err error) { func (client *restClient) Patch(url string, contentType string, body io.ReadSeeker, tokens *TokenOptions) (res *http.Response, err error) {
req := request{"DELETE", url, "", nil, token} if contentType == "" {
res, err = client.Do(&req) contentType = appJson
}
req := request{"PATCH", url, contentType, body, tokens}
rewinder := func() io.Reader {
body.Seek(0, 0)
return body
}
res, err = client.SendRequest(&req, rewinder)
return return
} }
func (client *restClient) Do(req *request) (res *http.Response, err error) { func (client *restClient) Put(url string, contentType string, body io.ReadSeeker, tokens *TokenOptions) (res *http.Response, err error) {
if contentType == "" {
contentType = appJson
}
req := request{"PUT", url, contentType, body, tokens}
rewinder := func() io.Reader {
body.Seek(0, 0)
return body
}
res, err = client.SendRequest(&req, rewinder)
return
}
func (client *restClient) Delete(url string, tokens *TokenOptions) (res *http.Response, err error) {
req := request{"DELETE", url, "", nil, tokens}
res, err = client.SendRequest(&req, nil)
return
}
func (client *restClient) SendRequest(req *request, bodyRewinder bodyRewinder) (res *http.Response, err error) {
res, err = client.sendRequestHelper(req)
// In most cases, we'll return immediately
// If the operation succeeded, but we got a 401 response and if we're using
// authentication, then we'll look into the body to see if the token expired
if err != nil {
return res, err
}
if res.StatusCode != 401 {
// It's not a 401, so the token didn't expire
return res, err
}
if req.Tokens == nil || req.Tokens.AccessToken == "" {
// We don't have a token, so we can't renew the token, no need to proceed
return res, err
}
// We're going to look in the body to see if it failed because the token expired
// This means we need to read the body, but the functions that call us also
// expect to read the body. So we read the body, then create a new reader
// so they can read the body as normal.
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return res, err
}
res.Body = ioutil.NopCloser(bytes.NewReader(body))
// Now see if we had an expired token or not
var apiError ApiError
err = json.Unmarshal(body, &apiError)
if err != nil {
return res, err
}
if apiError.Code != "ExpiredAuthToken" {
return res, nil
}
// We were told that the access token expired, so try to renew it.
// Note that this looks recursive because GetTokensByRefreshToken() will
// call the /auth API, and therefore SendRequest(). However, it calls
// without a token, so we avoid having a loop
newTokens, err := client.Auth.GetTokensByRefreshToken(req.Tokens.RefreshToken)
if err != nil {
return res, err
}
req.Tokens.AccessToken = newTokens.AccessToken
if client.UpdateAccessTokenCallback != nil {
client.UpdateAccessTokenCallback(newTokens.AccessToken)
}
if req.Body != nil && bodyRewinder != nil {
req.Body = bodyRewinder()
}
res, err = client.sendRequestHelper(req)
return res, nil
}
func (client *restClient) sendRequestHelper(req *request) (res *http.Response, err error) {
r, err := http.NewRequest(req.Method, req.URL, req.Body) r, err := http.NewRequest(req.Method, req.URL, req.Body)
if err != nil { if err != nil {
client.logger.Printf("An error occured creating request %s on %s. Error: %s", req.Method, req.URL, err) client.logger.Printf("An error occured creating request %s on %s. Error: %s", req.Method, req.URL, err)
...@@ -149,8 +246,8 @@ func (client *restClient) Do(req *request) (res *http.Response, err error) { ...@@ -149,8 +246,8 @@ func (client *restClient) Do(req *request) (res *http.Response, err error) {
if req.ContentType != "" { if req.ContentType != "" {
r.Header.Add("Content-Type", req.ContentType) r.Header.Add("Content-Type", req.ContentType)
} }
if req.Token != "" { if req.Tokens != nil && req.Tokens.AccessToken != "" {
r.Header.Add("Authorization", "Bearer "+req.Token) r.Header.Add("Authorization", "Bearer "+req.Tokens.AccessToken)
} }
res, err = client.httpClient.Do(r) res, err = client.httpClient.Do(r)
if err != nil { if err != nil {
...@@ -162,21 +259,33 @@ func (client *restClient) Do(req *request) (res *http.Response, err error) { ...@@ -162,21 +259,33 @@ func (client *restClient) Do(req *request) (res *http.Response, err error) {
return return
} }
func (client *restClient) MultipartUploadFile(url, filePath string, params map[string]string, token string) (res *http.Response, err error) { func (client *restClient) MultipartUploadFile(url, filePath string, params map[string]string, tokens *TokenOptions) (res *http.Response, err error) {
file, err := os.Open(filePath) file, err := os.Open(filePath)
if err != nil { if err != nil {
return return
} }
defer file.Close() defer file.Close()
return client.MultipartUpload(url, file, filepath.Base(filePath), params, token) return client.MultipartUpload(url, file, filepath.Base(filePath), params, tokens)
} }
func (client *restClient) MultipartUpload(url string, reader io.Reader, filename string, params map[string]string, token string) (res *http.Response, err error) { func (client *restClient) MultipartUpload(url string, reader io.ReadSeeker, filename string, params map[string]string, tokens *TokenOptions) (res *http.Response, err error) {
boundary := client.randomBoundary()
multiReader, contentType := client.createMultiReader(reader, filename, params, boundary)
rewinder := func() io.Reader {
reader.Seek(0, 0)
multiReader, _ := client.createMultiReader(reader, filename, params, boundary)
return multiReader
}
res, err = client.SendRequest(&request{"POST", url, contentType, multiReader, tokens}, rewinder)
return
}
func (client *restClient) createMultiReader(reader io.ReadSeeker, filename string, params map[string]string, boundary string) (io.Reader, string) {
// The mime/multipart package does not support streaming multipart data from disk, // The mime/multipart package does not support streaming multipart data from disk,
// at least not without complicated, problematic goroutines that simultaneously read/write into a buffer. // at least not without complicated, problematic goroutines that simultaneously read/write into a buffer.
// A much easier approach is to just construct the multipart request by hand, using io.MultiPart to // A much easier approach is to just construct the multipart request by hand, using io.MultiPart to
// concatenate the parts of the request together into a single io.Reader. // concatenate the parts of the request together into a single io.Reader.
boundary := client.randomBoundary()
parts := []io.Reader{} parts := []io.Reader{}
// Create a part for each key, val pair in params // Create a part for each key, val pair in params
...@@ -195,9 +304,7 @@ func (client *restClient) MultipartUpload(url string, reader io.Reader, filename ...@@ -195,9 +304,7 @@ func (client *restClient) MultipartUpload(url string, reader io.Reader, filename
contentType := fmt.Sprintf("multipart/form-data; boundary=%s", boundary) contentType := fmt.Sprintf("multipart/form-data; boundary=%s", boundary)
res, err = client.Do(&request{"POST", url, contentType, io.MultiReader(parts...), token}) return io.MultiReader(parts...), contentType
return
} }
// From https://golang.org/src/mime/multipart/writer.go // From https://golang.org/src/mime/multipart/writer.go
......
// Copyright (c) 2016 VMware, Inc. All Rights Reserved. // Copyright (c) 2017 VMware, Inc. All Rights Reserved.
// //
// This product is licensed to you under the Apache License, Version 2.0 (the "License"). // This product is licensed to you under the Apache License, Version 2.0 (the "License").
// You may not use this product except in compliance with the License. // You may not use this product except in compliance with the License.
...@@ -14,42 +14,16 @@ import ( ...@@ -14,42 +14,16 @@ import (
"encoding/json" "encoding/json"
) )
// Contains functionality for clusters API. // Contains functionality for routers API.
type ClustersAPI struct { type RoutersAPI struct {
client *Client client *Client
} }
var clusterUrl string = "/clusters/" var routerUrl string = rootUrl + "/routers/"
const ExtendedPropertyDNS string = "dns" // Gets a router with the specified ID.
const ExtendedPropertyGateway string = "gateway" func (api *RoutersAPI) Get(id string) (router *Router, err error) {
const ExtendedPropertyNetMask string = "netmask" res, err := api.client.restClient.Get(api.client.Endpoint+routerUrl+id, api.client.options.TokenOptions)
const ExtendedPropertyMasterIP string = "master_ip"
const ExtendedPropertyContainerNetwork string = "container_network"
const ExtendedPropertyZookeeperIP1 string = "zookeeper_ip1"
const ExtendedPropertyZookeeperIP2 string = "zookeeper_ip2"
const ExtendedPropertyZookeeperIP3 string = "zookeeper_ip3"
const ExtendedPropertyETCDIP1 string = "etcd_ip1"
const ExtendedPropertyETCDIP2 string = "etcd_ip2"
const ExtendedPropertyETCDIP3 string = "etcd_ip3"
const ExtendedPropertySSHKey string = "ssh_key"
const ExtendedPropertyRegistryCACert string = "registry_ca_cert"
const ExtendedPropertyAdminPassword string = "admin_password"
// Deletes a cluster with specified ID.
func (api *ClustersAPI) Delete(id string) (task *Task, err error) {
res, err := api.client.restClient.Delete(api.client.Endpoint+clusterUrl+id, api.client.options.TokenOptions.AccessToken)
if err != nil {
return
}
defer res.Body.Close()
task, err = getTask(getError(res))
return
}
// Gets a cluster with the specified ID.
func (api *ClustersAPI) Get(id string) (cluster *Cluster, err error) {
res, err := api.client.restClient.Get(api.client.Endpoint+clusterUrl+id, api.client.options.TokenOptions.AccessToken)
if err != nil { if err != nil {
return return
} }
...@@ -58,51 +32,55 @@ func (api *ClustersAPI) Get(id string) (cluster *Cluster, err error) { ...@@ -58,51 +32,55 @@ func (api *ClustersAPI) Get(id string) (cluster *Cluster, err error) {
if err != nil { if err != nil {
return return
} }
var result Cluster var result Router
err = json.NewDecoder(res.Body).Decode(&result) err = json.NewDecoder(res.Body).Decode(&result)
return &result, nil return &result, nil
} }
// Gets vms for cluster with the specified ID. // Updates router's attributes.
func (api *ClustersAPI) GetVMs(id string) (result *VMs, err error) { func (api *RoutersAPI) UpdateRouter(id string, routerSpec *RouterUpdateSpec) (task *Task, err error) {
uri := api.client.Endpoint + clusterUrl + id + "/vms" body, err := json.Marshal(routerSpec)
res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions.AccessToken)
if err != nil { if err != nil {
return return
} }
result = &VMs{} res, err := api.client.restClient.Put(
err = json.Unmarshal(res, result) api.client.Endpoint+routerUrl+id,
return "application/json",
} bytes.NewReader(body),
api.client.options.TokenOptions)
// Resize a cluster to specified count.
func (api *ClustersAPI) Resize(id string, resize *ClusterResizeOperation) (task *Task, err error) {
body, err := json.Marshal(resize)
if err != nil { if err != nil {
return return
} }
res, err := api.client.restClient.Post(
api.client.Endpoint+clusterUrl+id+"/resize", defer res.Body.Close()
"application/json", task, err = getTask(getError(res))
bytes.NewReader(body), return
api.client.options.TokenOptions.AccessToken) }
// Deletes a router with specified ID.
func (api *RoutersAPI) Delete(routerID string) (task *Task, err error) {
res, err := api.client.restClient.Delete(api.client.Endpoint+routerUrl+routerID, api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
defer res.Body.Close() defer res.Body.Close()
task, err = getTask(getError(res)) task, err = getTask(getError(res))
return return
} }
// Start a background process to recreate failed VMs in a cluster with the specified ID. // Creates a subnet on the specified router.
func (api *ClustersAPI) TriggerMaintenance(id string) (task *Task, err error) { func (api *RoutersAPI) CreateSubnet(routerID string, spec *SubnetCreateSpec) (task *Task, err error) {
body := []byte{} body, err := json.Marshal(spec)
if err != nil {
return
}
res, err := api.client.restClient.Post( res, err := api.client.restClient.Post(
api.client.Endpoint+clusterUrl+id+"/trigger_maintenance", api.client.Endpoint+routerUrl+routerID+"/subnets",
"application/json", "application/json",
bytes.NewReader(body), bytes.NewReader(body),
api.client.options.TokenOptions.AccessToken) api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
......
// Copyright (c) 2016 VMware, Inc. All Rights Reserved.
//
// This product is licensed to you under the Apache License, Version 2.0 (the "License").
// You may not use this product except in compliance with the License.
//
// This product may include a number of subcomponents with separate copyright notices and
// license terms. Your use of these subcomponents is subject to the terms and conditions
// of the subcomponent's license, as noted in the LICENSE file.
package photon
import (
"bytes"
"encoding/json"
)
// Contains functionality for services API.
type ServicesAPI struct {
client *Client
}
var serviceUrl = rootUrl + "/services/"
// Extended Properties
const (
ExtendedPropertyDNS = "dns"
ExtendedPropertyGateway = "gateway"
ExtendedPropertyNetMask = "netmask"
ExtendedPropertyLoadBalancerIP = "load_balancer_ip"
ExtendedPropertyNumberOfMasters = "number_of_masters"
ExtendedPropertyMasterIPs = "master_ips"
ExtendedPropertyMasterIP = "master_ip"
ExtendedPropertyMasterIP2 = "master_ip2"
ExtendedPropertyContainerNetwork = "container_network"
ExtendedPropertyZookeeperIP1 = "zookeeper_ip1"
ExtendedPropertyZookeeperIP2 = "zookeeper_ip2"
ExtendedPropertyZookeeperIP3 = "zookeeper_ip3"
ExtendedPropertyNumberOfETCDs = "number_of_etcds"
ExtendedPropertyETCDIP1 = "etcd_ip1"
ExtendedPropertyETCDIP2 = "etcd_ip2"
ExtendedPropertyETCDIP3 = "etcd_ip3"
ExtendedPropertySSHKey = "ssh_key"
ExtendedPropertyRegistryCACert = "registry_ca_cert"
ExtendedPropertyAdminPassword = "admin_password"
)
// Deletes a service with specified ID.
func (api *ServicesAPI) Delete(id string) (task *Task, err error) {
res, err := api.client.restClient.Delete(api.client.Endpoint+serviceUrl+id, api.client.options.TokenOptions)
if err != nil {
return
}
defer res.Body.Close()
task, err = getTask(getError(res))
return
}
// Gets a service with the specified ID.
func (api *ServicesAPI) Get(id string) (service *Service, err error) {
res, err := api.client.restClient.Get(api.client.Endpoint+serviceUrl+id, api.client.options.TokenOptions)
if err != nil {
return
}
defer res.Body.Close()
res, err = getError(res)
if err != nil {
return
}
var result Service
err = json.NewDecoder(res.Body).Decode(&result)
return &result, nil
}
// Gets vms for service with the specified ID.
func (api *ServicesAPI) GetVMs(id string) (result *VMs, err error) {
uri := api.client.Endpoint + serviceUrl + id + "/vms"
res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions)
if err != nil {
return
}
result = &VMs{}
err = json.Unmarshal(res, result)
return
}
// Resize a service to specified count.
func (api *ServicesAPI) Resize(id string, resize *ServiceResizeOperation) (task *Task, err error) {
body, err := json.Marshal(resize)
if err != nil {
return
}
res, err := api.client.restClient.Post(
api.client.Endpoint+serviceUrl+id+"/resize",
"application/json",
bytes.NewReader(body),
api.client.options.TokenOptions)
if err != nil {
return
}
defer res.Body.Close()
task, err = getTask(getError(res))
return
}
// Start a background process to recreate failed VMs in a service with the specified ID.
func (api *ServicesAPI) TriggerMaintenance(id string) (task *Task, err error) {
body := []byte{}
res, err := api.client.restClient.Post(
api.client.Endpoint+serviceUrl+id+"/trigger_maintenance",
"application/json",
bytes.NewReader(body),
api.client.options.TokenOptions)
if err != nil {
return
}
defer res.Body.Close()
task, err = getTask(getError(res))
return
}
// Change a service version to the specified image by destroying and recreating the VMs.
func (api *ServicesAPI) ChangeVersion(id string, changeVersion *ServiceChangeVersionOperation) (task *Task, err error) {
body, err := json.Marshal(changeVersion)
if err != nil {
return
}
res, err := api.client.restClient.Post(
api.client.Endpoint+serviceUrl+id+"/change_version",
"application/json",
bytes.NewReader(body),
api.client.options.TokenOptions)
if err != nil {
return
}
defer res.Body.Close()
task, err = getTask(getError(res))
return
}
...@@ -18,11 +18,11 @@ type StatusAPI struct { ...@@ -18,11 +18,11 @@ type StatusAPI struct {
client *Client client *Client
} }
var statusUrl string = "/status" var statusUrl string = rootUrl + "/status"
// Returns the status of an photon endpoint. // Returns the status of an photon endpoint.
func (api *StatusAPI) Get() (status *Status, err error) { func (api *StatusAPI) Get() (status *Status, err error) {
res, err := api.client.restClient.Get(api.client.Endpoint+statusUrl, api.client.options.TokenOptions.AccessToken) res, err := api.client.restClient.Get(api.client.Endpoint+statusUrl, api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
......
// Copyright (c) 2017 VMware, Inc. All Rights Reserved.
//
// This product is licensed to you under the Apache License, Version 2.0 (the "License").
// You may not use this product except in compliance with the License.
//
// This product may include a number of subcomponents with separate copyright notices and
// license terms. Your use of these subcomponents is subject to the terms and conditions
// of the subcomponent's license, as noted in the LICENSE file.
package photon
import (
"bytes"
"encoding/json"
)
// Contains functionality for subnets API.
type SubnetsAPI struct {
client *Client
}
var subnetUrl string = "/temp-subnets/"
// Deletes a subnet with the specified ID.
func (api *SubnetsAPI) Delete(id string) (task *Task, err error) {
res, err := api.client.restClient.Delete(api.client.Endpoint+subnetUrl+id, api.client.options.TokenOptions)
if err != nil {
return
}
defer res.Body.Close()
task, err = getTask(getError(res))
return
}
// Gets a subnet with the specified ID.
func (api *SubnetsAPI) Get(id string) (subnet *Subnet, err error) {
res, err := api.client.restClient.Get(api.client.Endpoint+subnetUrl+id, api.client.options.TokenOptions)
if err != nil {
return
}
defer res.Body.Close()
res, err = getError(res)
if err != nil {
return
}
var result Subnet
err = json.NewDecoder(res.Body).Decode(&result)
return &result, nil
}
// Updates subnet's attributes.
func (api *SubnetsAPI) Update(id string, subnetSpec *SubnetUpdateSpec) (task *Task, err error) {
body, err := json.Marshal(subnetSpec)
if err != nil {
return
}
res, err := api.client.restClient.Put(
api.client.Endpoint+subnetUrl+id,
"application/json",
bytes.NewReader(body),
api.client.options.TokenOptions)
if err != nil {
return
}
defer res.Body.Close()
task, err = getTask(getError(res))
return
}
...@@ -19,11 +19,11 @@ type TasksAPI struct { ...@@ -19,11 +19,11 @@ type TasksAPI struct {
client *Client client *Client
} }
var taskUrl string = "/tasks" var taskUrl string = rootUrl + "/tasks"
// Gets a task by ID. // Gets a task by ID.
func (api *TasksAPI) Get(id string) (task *Task, err error) { func (api *TasksAPI) Get(id string) (task *Task, err error) {
res, err := api.client.restClient.Get(api.client.Endpoint+taskUrl+"/"+id, api.client.options.TokenOptions.AccessToken) res, err := api.client.restClient.Get(api.client.Endpoint+taskUrl+"/"+id, api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -39,7 +39,7 @@ func (api *TasksAPI) GetAll(options *TaskGetOptions) (result *TaskList, err erro ...@@ -39,7 +39,7 @@ func (api *TasksAPI) GetAll(options *TaskGetOptions) (result *TaskList, err erro
if options != nil { if options != nil {
uri += getQueryString(options) uri += getQueryString(options)
} }
res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions.AccessToken) res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
......
...@@ -12,6 +12,7 @@ package photon ...@@ -12,6 +12,7 @@ package photon
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt"
) )
// Contains functionality for tenants API. // Contains functionality for tenants API.
...@@ -29,12 +30,12 @@ type ProjectGetOptions struct { ...@@ -29,12 +30,12 @@ type ProjectGetOptions struct {
Name string `urlParam:"name"` Name string `urlParam:"name"`
} }
var tenantUrl string = "/tenants" var tenantUrl string = rootUrl + "/tenants"
// Returns all tenants on an photon instance. // Returns all tenants on an photon instance.
func (api *TenantsAPI) GetAll() (result *Tenants, err error) { func (api *TenantsAPI) GetAll() (result *Tenants, err error) {
uri := api.client.Endpoint + tenantUrl uri := api.client.Endpoint + tenantUrl
res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions.AccessToken) res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -54,7 +55,7 @@ func (api *TenantsAPI) Create(tenantSpec *TenantCreateSpec) (task *Task, err err ...@@ -54,7 +55,7 @@ func (api *TenantsAPI) Create(tenantSpec *TenantCreateSpec) (task *Task, err err
api.client.Endpoint+tenantUrl, api.client.Endpoint+tenantUrl,
"application/json", "application/json",
bytes.NewReader(body), bytes.NewReader(body),
api.client.options.TokenOptions.AccessToken) api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -65,7 +66,7 @@ func (api *TenantsAPI) Create(tenantSpec *TenantCreateSpec) (task *Task, err err ...@@ -65,7 +66,7 @@ func (api *TenantsAPI) Create(tenantSpec *TenantCreateSpec) (task *Task, err err
// Deletes the tenant with specified ID. Any projects, VMs, disks, etc., owned by the tenant must be deleted first. // Deletes the tenant with specified ID. Any projects, VMs, disks, etc., owned by the tenant must be deleted first.
func (api *TenantsAPI) Delete(id string) (task *Task, err error) { func (api *TenantsAPI) Delete(id string) (task *Task, err error) {
res, err := api.client.restClient.Delete(api.client.Endpoint+tenantUrl+"/"+id, api.client.options.TokenOptions.AccessToken) res, err := api.client.restClient.Delete(api.client.Endpoint+tenantUrl+"/"+id, api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -84,7 +85,7 @@ func (api *TenantsAPI) CreateResourceTicket(tenantId string, spec *ResourceTicke ...@@ -84,7 +85,7 @@ func (api *TenantsAPI) CreateResourceTicket(tenantId string, spec *ResourceTicke
api.client.Endpoint+tenantUrl+"/"+tenantId+"/resource-tickets", api.client.Endpoint+tenantUrl+"/"+tenantId+"/resource-tickets",
"application/json", "application/json",
bytes.NewReader(body), bytes.NewReader(body),
api.client.options.TokenOptions.AccessToken) api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -100,7 +101,7 @@ func (api *TenantsAPI) GetResourceTickets(tenantId string, options *ResourceTick ...@@ -100,7 +101,7 @@ func (api *TenantsAPI) GetResourceTickets(tenantId string, options *ResourceTick
if options != nil { if options != nil {
uri += getQueryString(options) uri += getQueryString(options)
} }
res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions.AccessToken) res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -120,7 +121,7 @@ func (api *TenantsAPI) CreateProject(tenantId string, spec *ProjectCreateSpec) ( ...@@ -120,7 +121,7 @@ func (api *TenantsAPI) CreateProject(tenantId string, spec *ProjectCreateSpec) (
api.client.Endpoint+tenantUrl+"/"+tenantId+"/projects", api.client.Endpoint+tenantUrl+"/"+tenantId+"/projects",
"application/json", "application/json",
bytes.NewReader(body), bytes.NewReader(body),
api.client.options.TokenOptions.AccessToken) api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -136,7 +137,7 @@ func (api *TenantsAPI) GetProjects(tenantId string, options *ProjectGetOptions) ...@@ -136,7 +137,7 @@ func (api *TenantsAPI) GetProjects(tenantId string, options *ProjectGetOptions)
if options != nil { if options != nil {
uri += getQueryString(options) uri += getQueryString(options)
} }
res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions.AccessToken) res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -153,7 +154,7 @@ func (api *TenantsAPI) GetTasks(id string, options *TaskGetOptions) (result *Tas ...@@ -153,7 +154,7 @@ func (api *TenantsAPI) GetTasks(id string, options *TaskGetOptions) (result *Tas
if options != nil { if options != nil {
uri += getQueryString(options) uri += getQueryString(options)
} }
res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions.AccessToken) res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -163,19 +164,42 @@ func (api *TenantsAPI) GetTasks(id string, options *TaskGetOptions) (result *Tas ...@@ -163,19 +164,42 @@ func (api *TenantsAPI) GetTasks(id string, options *TaskGetOptions) (result *Tas
return return
} }
// Gets a tenant with the specified ID. // Gets a tenant with the specified ID or name
func (api *TenantsAPI) Get(id string) (tenant *Tenant, err error) { func (api *TenantsAPI) Get(identity string) (tenant *Tenant, err error) {
res, err := api.client.restClient.Get(api.getEntityUrl(id), api.client.options.TokenOptions.AccessToken) res, err := api.client.restClient.Get(api.getEntityUrl(identity), api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
defer res.Body.Close() defer res.Body.Close()
res, err = getError(res) res, err = getError(res)
tenant = &Tenant{}
if res != nil {
err = json.NewDecoder(res.Body).Decode(tenant)
// ID corresponds to the tenant ID found, return tenant
if err == nil {
return
}
}
// Find by Name
uri := api.client.Endpoint + tenantUrl + "?name=" + identity
res2, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
tenant = &Tenant{}
err = json.NewDecoder(res.Body).Decode(tenant) tenants := &Tenants{}
err = json.Unmarshal(res2, tenants)
if err != nil {
return
}
if len(tenants.Items) < 1 {
err = fmt.Errorf("Cannot find a tenant with id or name match %s", identity)
return
}
tenant = &(tenants.Items[0])
return return
} }
......
...@@ -100,7 +100,7 @@ func setSecurityGroups(client *Client, entityUrl string, securityGroups *Securit ...@@ -100,7 +100,7 @@ func setSecurityGroups(client *Client, entityUrl string, securityGroups *Securit
url, url,
"application/json", "application/json",
bytes.NewReader(body), bytes.NewReader(body),
client.options.TokenOptions.AccessToken) client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
......
...@@ -24,8 +24,8 @@ type VirtualSubnetGetOptions struct { ...@@ -24,8 +24,8 @@ type VirtualSubnetGetOptions struct {
Name string `urlParam:"name"` Name string `urlParam:"name"`
} }
var subnetsUrl = "/subnets" var subnetsUrl = rootUrl + "/subnets"
var projectsUrl = "/projects" var projectsUrl = rootUrl + "/projects"
// Create a virtual network // Create a virtual network
func (api *VirtualSubnetsAPI) Create(projectId string, func (api *VirtualSubnetsAPI) Create(projectId string,
...@@ -39,8 +39,8 @@ func (api *VirtualSubnetsAPI) Create(projectId string, ...@@ -39,8 +39,8 @@ func (api *VirtualSubnetsAPI) Create(projectId string,
res, err := api.client.restClient.Post( res, err := api.client.restClient.Post(
api.client.Endpoint+projectsUrl+"/"+projectId+"/subnets", api.client.Endpoint+projectsUrl+"/"+projectId+"/subnets",
"application/json", "application/json",
bytes.NewBuffer(body), bytes.NewReader(body),
api.client.options.TokenOptions.AccessToken) api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -54,7 +54,7 @@ func (api *VirtualSubnetsAPI) Create(projectId string, ...@@ -54,7 +54,7 @@ func (api *VirtualSubnetsAPI) Create(projectId string,
// Delete a virtual network with the specified ID. // Delete a virtual network with the specified ID.
func (api *VirtualSubnetsAPI) Delete(id string) (task *Task, err error) { func (api *VirtualSubnetsAPI) Delete(id string) (task *Task, err error) {
res, err := api.client.restClient.Delete(api.client.Endpoint+subnetsUrl+"/"+id, res, err := api.client.restClient.Delete(api.client.Endpoint+subnetsUrl+"/"+id,
api.client.options.TokenOptions.AccessToken) api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -68,7 +68,7 @@ func (api *VirtualSubnetsAPI) Delete(id string) (task *Task, err error) { ...@@ -68,7 +68,7 @@ func (api *VirtualSubnetsAPI) Delete(id string) (task *Task, err error) {
// Get the virtual subnet with the specified id // Get the virtual subnet with the specified id
func (api *VirtualSubnetsAPI) Get(id string) (subnet *VirtualSubnet, err error) { func (api *VirtualSubnetsAPI) Get(id string) (subnet *VirtualSubnet, err error) {
res, err := api.client.restClient.Get(api.client.Endpoint+subnetsUrl+"/"+id, res, err := api.client.restClient.Get(api.client.Endpoint+subnetsUrl+"/"+id,
api.client.options.TokenOptions.AccessToken) api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -94,7 +94,7 @@ func (api *VirtualSubnetsAPI) GetAll(projectId string, ...@@ -94,7 +94,7 @@ func (api *VirtualSubnetsAPI) GetAll(projectId string,
uri += getQueryString(options) uri += getQueryString(options)
} }
res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions.AccessToken) res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -109,8 +109,8 @@ func (api *VirtualSubnetsAPI) SetDefault(id string) (task *Task, err error) { ...@@ -109,8 +109,8 @@ func (api *VirtualSubnetsAPI) SetDefault(id string) (task *Task, err error) {
res, err := api.client.restClient.Post( res, err := api.client.restClient.Post(
api.client.Endpoint+subnetsUrl+"/"+id+"/set_default", api.client.Endpoint+subnetsUrl+"/"+id+"/set_default",
"application/json", "application/json",
bytes.NewBuffer([]byte("")), bytes.NewReader([]byte("")),
api.client.options.TokenOptions.AccessToken) api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
......
...@@ -20,10 +20,10 @@ type VmAPI struct { ...@@ -20,10 +20,10 @@ type VmAPI struct {
client *Client client *Client
} }
var vmUrl string = "/vms/" var vmUrl string = rootUrl + "/vms/"
func (api *VmAPI) Get(id string) (vm *VM, err error) { func (api *VmAPI) Get(id string) (vm *VM, err error) {
res, err := api.client.restClient.Get(api.client.Endpoint+vmUrl+id, api.client.options.TokenOptions.AccessToken) res, err := api.client.restClient.Get(api.client.Endpoint+vmUrl+id, api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -38,7 +38,7 @@ func (api *VmAPI) Get(id string) (vm *VM, err error) { ...@@ -38,7 +38,7 @@ func (api *VmAPI) Get(id string) (vm *VM, err error) {
} }
func (api *VmAPI) Delete(id string) (task *Task, err error) { func (api *VmAPI) Delete(id string) (task *Task, err error) {
res, err := api.client.restClient.Delete(api.client.Endpoint+vmUrl+id, api.client.options.TokenOptions.AccessToken) res, err := api.client.restClient.Delete(api.client.Endpoint+vmUrl+id, api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
...@@ -57,7 +57,7 @@ func (api *VmAPI) AttachDisk(id string, op *VmDiskOperation) (task *Task, err er ...@@ -57,7 +57,7 @@ func (api *VmAPI) AttachDisk(id string, op *VmDiskOperation) (task *Task, err er
api.client.Endpoint+vmUrl+id+"/attach_disk", api.client.Endpoint+vmUrl+id+"/attach_disk",
"application/json", "application/json",
bytes.NewReader(body), bytes.NewReader(body),
api.client.options.TokenOptions.AccessToken) api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -75,7 +75,7 @@ func (api *VmAPI) DetachDisk(id string, op *VmDiskOperation) (task *Task, err er ...@@ -75,7 +75,7 @@ func (api *VmAPI) DetachDisk(id string, op *VmDiskOperation) (task *Task, err er
api.client.Endpoint+vmUrl+id+"/detach_disk", api.client.Endpoint+vmUrl+id+"/detach_disk",
"application/json", "application/json",
bytes.NewReader(body), bytes.NewReader(body),
api.client.options.TokenOptions.AccessToken) api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -84,9 +84,9 @@ func (api *VmAPI) DetachDisk(id string, op *VmDiskOperation) (task *Task, err er ...@@ -84,9 +84,9 @@ func (api *VmAPI) DetachDisk(id string, op *VmDiskOperation) (task *Task, err er
return return
} }
func (api *VmAPI) AttachISO(id string, reader io.Reader, name string) (task *Task, err error) { func (api *VmAPI) AttachISO(id string, reader io.ReadSeeker, name string) (task *Task, err error) {
res, err := api.client.restClient.MultipartUpload( res, err := api.client.restClient.MultipartUpload(
api.client.Endpoint+vmUrl+id+"/attach_iso", reader, name, nil, api.client.options.TokenOptions.AccessToken) api.client.Endpoint+vmUrl+id+"/attach_iso", reader, name, nil, api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -104,7 +104,7 @@ func (api *VmAPI) DetachISO(id string) (task *Task, err error) { ...@@ -104,7 +104,7 @@ func (api *VmAPI) DetachISO(id string) (task *Task, err error) {
api.client.Endpoint+vmUrl+id+"/detach_iso", api.client.Endpoint+vmUrl+id+"/detach_iso",
"application/json", "application/json",
bytes.NewReader(body), bytes.NewReader(body),
api.client.options.TokenOptions.AccessToken) api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -122,7 +122,7 @@ func (api *VmAPI) Start(id string) (task *Task, err error) { ...@@ -122,7 +122,7 @@ func (api *VmAPI) Start(id string) (task *Task, err error) {
api.client.Endpoint+vmUrl+id+"/start", api.client.Endpoint+vmUrl+id+"/start",
"application/json", "application/json",
bytes.NewReader(body), bytes.NewReader(body),
api.client.options.TokenOptions.AccessToken) api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -140,7 +140,7 @@ func (api *VmAPI) Stop(id string) (task *Task, err error) { ...@@ -140,7 +140,7 @@ func (api *VmAPI) Stop(id string) (task *Task, err error) {
api.client.Endpoint+vmUrl+id+"/stop", api.client.Endpoint+vmUrl+id+"/stop",
"application/json", "application/json",
bytes.NewReader(body), bytes.NewReader(body),
api.client.options.TokenOptions.AccessToken) api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -158,7 +158,7 @@ func (api *VmAPI) Restart(id string) (task *Task, err error) { ...@@ -158,7 +158,7 @@ func (api *VmAPI) Restart(id string) (task *Task, err error) {
api.client.Endpoint+vmUrl+id+"/restart", api.client.Endpoint+vmUrl+id+"/restart",
"application/json", "application/json",
bytes.NewReader(body), bytes.NewReader(body),
api.client.options.TokenOptions.AccessToken) api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -176,7 +176,7 @@ func (api *VmAPI) Resume(id string) (task *Task, err error) { ...@@ -176,7 +176,7 @@ func (api *VmAPI) Resume(id string) (task *Task, err error) {
api.client.Endpoint+vmUrl+id+"/resume", api.client.Endpoint+vmUrl+id+"/resume",
"application/json", "application/json",
bytes.NewReader(body), bytes.NewReader(body),
api.client.options.TokenOptions.AccessToken) api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -194,7 +194,7 @@ func (api *VmAPI) Suspend(id string) (task *Task, err error) { ...@@ -194,7 +194,7 @@ func (api *VmAPI) Suspend(id string) (task *Task, err error) {
api.client.Endpoint+vmUrl+id+"/suspend", api.client.Endpoint+vmUrl+id+"/suspend",
"application/json", "application/json",
bytes.NewReader(body), bytes.NewReader(body),
api.client.options.TokenOptions.AccessToken) api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -212,7 +212,7 @@ func (api *VmAPI) SetMetadata(id string, metadata *VmMetadata) (task *Task, err ...@@ -212,7 +212,7 @@ func (api *VmAPI) SetMetadata(id string, metadata *VmMetadata) (task *Task, err
api.client.Endpoint+vmUrl+id+"/set_metadata", api.client.Endpoint+vmUrl+id+"/set_metadata",
"application/json", "application/json",
bytes.NewReader(body), bytes.NewReader(body),
api.client.options.TokenOptions.AccessToken) api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -228,7 +228,7 @@ func (api *VmAPI) GetTasks(id string, options *TaskGetOptions) (result *TaskList ...@@ -228,7 +228,7 @@ func (api *VmAPI) GetTasks(id string, options *TaskGetOptions) (result *TaskList
if options != nil { if options != nil {
uri += getQueryString(options) uri += getQueryString(options)
} }
res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions.AccessToken) res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -239,7 +239,7 @@ func (api *VmAPI) GetTasks(id string, options *TaskGetOptions) (result *TaskList ...@@ -239,7 +239,7 @@ func (api *VmAPI) GetTasks(id string, options *TaskGetOptions) (result *TaskList
} }
func (api *VmAPI) GetNetworks(id string) (task *Task, err error) { func (api *VmAPI) GetNetworks(id string) (task *Task, err error) {
res, err := api.client.restClient.Get(api.client.Endpoint+vmUrl+id+"/subnets", api.client.options.TokenOptions.AccessToken) res, err := api.client.restClient.Get(api.client.Endpoint+vmUrl+id+"/subnets", api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -258,7 +258,7 @@ func (api *VmAPI) AcquireFloatingIp(id string, spec *VmFloatingIpSpec) (task *Ta ...@@ -258,7 +258,7 @@ func (api *VmAPI) AcquireFloatingIp(id string, spec *VmFloatingIpSpec) (task *Ta
api.client.Endpoint+vmUrl+id+"/acquire_floating_ip", api.client.Endpoint+vmUrl+id+"/acquire_floating_ip",
"application/json", "application/json",
bytes.NewReader(body), bytes.NewReader(body),
api.client.options.TokenOptions.AccessToken) api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -271,7 +271,7 @@ func (api *VmAPI) ReleaseFloatingIp(id string) (task *Task, err error) { ...@@ -271,7 +271,7 @@ func (api *VmAPI) ReleaseFloatingIp(id string) (task *Task, err error) {
res, err := api.client.restClient.Delete( res, err := api.client.restClient.Delete(
api.client.Endpoint+vmUrl+id+"/release_floating_ip", api.client.Endpoint+vmUrl+id+"/release_floating_ip",
api.client.options.TokenOptions.AccessToken) api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -281,7 +281,7 @@ func (api *VmAPI) ReleaseFloatingIp(id string) (task *Task, err error) { ...@@ -281,7 +281,7 @@ func (api *VmAPI) ReleaseFloatingIp(id string) (task *Task, err error) {
} }
func (api *VmAPI) GetMKSTicket(id string) (task *Task, err error) { func (api *VmAPI) GetMKSTicket(id string) (task *Task, err error) {
res, err := api.client.restClient.Get(api.client.Endpoint+vmUrl+id+"/mks_ticket", api.client.options.TokenOptions.AccessToken) res, err := api.client.restClient.Get(api.client.Endpoint+vmUrl+id+"/mks_ticket", api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -299,7 +299,7 @@ func (api *VmAPI) SetTag(id string, tag *VmTag) (task *Task, err error) { ...@@ -299,7 +299,7 @@ func (api *VmAPI) SetTag(id string, tag *VmTag) (task *Task, err error) {
api.client.Endpoint+vmUrl+id+"/tags", api.client.Endpoint+vmUrl+id+"/tags",
"application/json", "application/json",
bytes.NewReader(body), bytes.NewReader(body),
api.client.options.TokenOptions.AccessToken) api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
...@@ -317,7 +317,7 @@ func (api *VmAPI) CreateImage(id string, options *ImageCreateSpec) (task *Task, ...@@ -317,7 +317,7 @@ func (api *VmAPI) CreateImage(id string, options *ImageCreateSpec) (task *Task,
api.client.Endpoint+vmUrl+id+"/create_image", api.client.Endpoint+vmUrl+id+"/create_image",
"application/json", "application/json",
bytes.NewReader(body), bytes.NewReader(body),
api.client.options.TokenOptions.AccessToken) api.client.options.TokenOptions)
if err != nil { if err != nil {
return return
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment