Merge pull request #694 in YUNIONIO/onecloud from ~LIZEXI/onecloud:feature/lzx-climc-manager-apiv to release/2.4.0

* commit 'c13c7da0a2bc07bf8969e49c30a1ffcce02a7cc7':
  climc: set default manager api version
This commit is contained in:
李泽玺
2018-12-08 12:37:16 +08:00
6 changed files with 86 additions and 25 deletions
+4 -3
View File
@@ -42,7 +42,7 @@ type BaseOptions struct {
OsRegionName string `default:"$OS_REGION_NAME" help:"Defaults to env[OS_REGION_NAME]"`
OsZoneName string `default:"$OS_ZONE_NAME" help:"Defaults to env[OS_ZONE_NAME]"`
OsEndpointType string `default:"$OS_ENDPOINT_TYPE|internalURL" help:"Defaults to env[OS_ENDPOINT_TYPE] or internalURL" choices:"publicURL|internalURL|adminURL"`
ApiVersion string `default:"$API_VERSION|v1" help:"apiVersion, default to v1"`
ApiVersion string `default:"$API_VERSION" help:"override default modules service api version"`
SUBCOMMAND string `help:"climc subcommand" subcommand:"true"`
}
@@ -178,10 +178,11 @@ func newClientSession(options *BaseOptions) (*mcclient.ClientSession, error) {
fo.Close()
}
}
} else {
// fmt.Printf("******** Use Token Cache At %s ********\n", tokenCachePath)
}
if options.ApiVersion != "" {
mcclient.DisableApiVersionByModule()
}
session := client.NewSession(options.OsRegionName,
options.OsZoneName,
options.OsEndpointType,
+2 -2
View File
@@ -339,8 +339,8 @@ func (this *Client) NewSession(region, zone, endpointType string, token TokenCre
}
return &ClientSession{client: this, region: region, zone: zone,
endpointType: endpointType, token: token,
apiVersion: apiVersion,
Header: http.Header{}}
defaultApiVersion: apiVersion,
Header: http.Header{}}
}
/*
+14 -4
View File
@@ -16,6 +16,8 @@ type BaseManager struct {
serviceType string
endpointType string
version string
apiVersion string
columns []string
adminColumns []string
}
@@ -42,6 +44,14 @@ func (this *BaseManager) SetVersion(v string) {
this.version = v
}
func (this *BaseManager) SetApiVersion(v string) {
this.apiVersion = v
}
func (this *BaseManager) GetApiVersion() string {
return this.apiVersion
}
func (this *BaseManager) versionedURL(path string) string {
offset := 0
for ; path[offset] == '/'; offset++ {
@@ -59,17 +69,17 @@ func (this *BaseManager) versionedURL(path string) string {
func (this *BaseManager) jsonRequest(session *mcclient.ClientSession,
method string, path string,
header http.Header, body jsonutils.JSONObject) (http.Header, jsonutils.JSONObject, error) {
return session.JSONRequest(this.serviceType, this.endpointType,
return session.JSONVersionRequest(this.serviceType, this.endpointType,
method, this.versionedURL(path),
header, body)
header, body, this.GetApiVersion())
}
func (this *BaseManager) rawRequest(session *mcclient.ClientSession,
method string, path string,
header http.Header, body io.Reader) (*http.Response, error) {
return session.RawRequest(this.serviceType, this.endpointType,
return session.RawVersionRequest(this.serviceType, this.endpointType,
method, this.versionedURL(path),
header, body)
header, body, this.GetApiVersion())
}
type ListResult struct {
+1
View File
@@ -16,6 +16,7 @@ type BaseManagerInterface interface {
EndpointType() string
GetColumns(session *mcclient.ClientSession) []string
List(session *mcclient.ClientSession, params jsonutils.JSONObject) (*ListResult, error)
SetApiVersion(string)
}
type ManagerContext struct {
+3
View File
@@ -1,5 +1,7 @@
package modules
import "yunion.io/x/onecloud/pkg/mcclient"
func registerCompute(mod BaseManagerInterface) {
registerComputeV1(mod)
registerComputeV2(mod)
@@ -10,6 +12,7 @@ func registerComputeV1(mod BaseManagerInterface) {
}
func registerComputeV2(mod BaseManagerInterface) {
mod.SetApiVersion(mcclient.V2_API_VERSION)
_register("v2", mod)
}
+62 -16
View File
@@ -21,21 +21,32 @@ const (
REGION_VERSION = "X-Region-Version"
DEFAULT_API_VERSION = "v1"
V2_API_VERSION = "v2"
)
var (
MutilVersionService = []string{"compute"}
ApiVersionByModule = true
)
func DisableApiVersionByModule() {
ApiVersionByModule = false
}
func EnableApiVersionByModule() {
ApiVersionByModule = true
}
type ClientSession struct {
client *Client
region string
zone string
endpointType string
apiVersion string
token TokenCredential
Header http.Header /// headers for this session
notifyChannel chan string
defaultApiVersion string
}
func populateHeader(self *http.Header, update http.Header) {
@@ -50,7 +61,7 @@ func populateHeader(self *http.Header, update http.Header) {
func GetTokenHeaders(userCred TokenCredential) http.Header {
headers := http.Header{}
headers.Set(AUTH_TOKEN, userCred.GetTokenString())
headers.Set(REGION_VERSION, "v2")
headers.Set(REGION_VERSION, V2_API_VERSION)
return headers
}
@@ -81,14 +92,30 @@ func (this *ClientSession) GetClient() *Client {
return this.client
}
func (this *ClientSession) getServiceName(service, apiVersion string) string {
if utils.IsInStringArray(service, MutilVersionService) && len(apiVersion) > 0 && apiVersion != DEFAULT_API_VERSION {
service = fmt.Sprintf("%s_%s", service, apiVersion)
}
return service
}
func (this *ClientSession) getApiVersion(moduleApiVersion string) string {
if moduleApiVersion != "" && ApiVersionByModule {
return moduleApiVersion
}
return this.defaultApiVersion
}
func (this *ClientSession) GetServiceURL(service, endpointType string) (string, error) {
return this.GetServiceVersionURL(service, endpointType, this.getApiVersion(""))
}
func (this *ClientSession) GetServiceVersionURL(service, endpointType, apiVersion string) (string, error) {
if len(this.endpointType) > 0 {
// session specific endpoint type should override the input endpointType, which is supplied by manager
endpointType = this.endpointType
}
if utils.IsInStringArray(service, MutilVersionService) && len(this.apiVersion) > 0 && this.apiVersion != DEFAULT_API_VERSION {
service = fmt.Sprintf("%s_%s", service, this.apiVersion)
}
service = this.getServiceName(service, apiVersion)
url, err := this.token.GetServiceURL(service, this.region, this.zone, endpointType)
if err != nil {
url, err = this.client.serviceCatalog.GetServiceURL(service, this.region, this.zone, endpointType)
@@ -97,13 +124,15 @@ func (this *ClientSession) GetServiceURL(service, endpointType string) (string,
}
func (this *ClientSession) GetServiceURLs(service, endpointType string) ([]string, error) {
return this.GetServiceVersionURLs(service, endpointType, this.getApiVersion(""))
}
func (this *ClientSession) GetServiceVersionURLs(service, endpointType, apiVersion string) ([]string, error) {
if len(this.endpointType) > 0 {
// session specific endpoint type should override the input endpointType, which is supplied by manager
endpointType = this.endpointType
}
if utils.IsInStringArray(service, MutilVersionService) && len(this.apiVersion) > 0 && this.apiVersion != DEFAULT_API_VERSION {
service = fmt.Sprintf("%s_%s", service, this.apiVersion)
}
service = this.getServiceName(service, apiVersion)
urls, err := this.token.GetServiceURLs(service, this.region, this.zone, endpointType)
if err != nil {
urls, err = this.client.serviceCatalog.GetServiceURLs(service, this.region, this.zone, endpointType)
@@ -111,20 +140,24 @@ func (this *ClientSession) GetServiceURLs(service, endpointType string) ([]strin
return urls, err
}
func (this *ClientSession) getBaseUrl(service, endpointType string) (string, error) {
func (this *ClientSession) getBaseUrl(service, endpointType, apiVersion string) (string, error) {
if len(service) > 0 {
if strings.HasPrefix(service, "http://") || strings.HasPrefix(service, "https://") {
return service, nil
} else {
return this.GetServiceURL(service, endpointType)
return this.GetServiceVersionURL(service, endpointType, this.getApiVersion(apiVersion))
}
} else {
return "", fmt.Errorf("Empty service type or baseURL")
}
}
func (this *ClientSession) RawRequest(service, endpointType, method, url string, headers http.Header, body io.Reader) (*http.Response, error) {
baseurl, err := this.getBaseUrl(service, endpointType)
func (this *ClientSession) RawVersionRequest(
service, endpointType, method, url string,
headers http.Header, body io.Reader,
apiVersion string,
) (*http.Response, error) {
baseurl, err := this.getBaseUrl(service, endpointType, apiVersion)
if err != nil {
return nil, err
}
@@ -138,8 +171,16 @@ func (this *ClientSession) RawRequest(service, endpointType, method, url string,
method, url, tmpHeader, body)
}
func (this *ClientSession) JSONRequest(service, endpointType, method, url string, headers http.Header, body jsonutils.JSONObject) (http.Header, jsonutils.JSONObject, error) {
baseUrl, err := this.getBaseUrl(service, endpointType)
func (this *ClientSession) RawRequest(service, endpointType, method, url string, headers http.Header, body io.Reader) (*http.Response, error) {
return this.RawVersionRequest(service, endpointType, method, url, headers, body, "")
}
func (this *ClientSession) JSONVersionRequest(
service, endpointType, method, url string,
headers http.Header, body jsonutils.JSONObject,
apiVersion string,
) (http.Header, jsonutils.JSONObject, error) {
baseUrl, err := this.getBaseUrl(service, endpointType, apiVersion)
if err != nil {
return headers, nil, err
}
@@ -153,6 +194,10 @@ func (this *ClientSession) JSONRequest(service, endpointType, method, url string
method, url, tmpHeader, body)
}
func (this *ClientSession) JSONRequest(service, endpointType, method, url string, headers http.Header, body jsonutils.JSONObject) (http.Header, jsonutils.JSONObject, error) {
return this.JSONVersionRequest(service, endpointType, method, url, headers, body, "")
}
func (this *ClientSession) ParseJSONResponse(resp *http.Response, err error) (http.Header, jsonutils.JSONObject, error) {
return httputils.ParseJSONResponse(resp, err, this.client.debug)
}
@@ -226,10 +271,11 @@ func (this *ClientSession) WaitTaskNotify() {
}
func (this *ClientSession) GetApiVersion() string {
if len(this.apiVersion) == 0 {
apiVersion := this.getApiVersion("")
if len(apiVersion) == 0 {
return DEFAULT_API_VERSION
}
return this.apiVersion
return apiVersion
}
func (this *ClientSession) ToJson() jsonutils.JSONObject {