Skip to content
Permalink
Browse files
adds support for custom services (#82)
  • Loading branch information
andrestc authored and Sander van Harmelen committed Oct 5, 2017
1 parent 2d98634 commit 4b447b18676518e48ac5e544115136180acff4f0
Showing 3 changed files with 116 additions and 0 deletions.
@@ -0,0 +1,64 @@
//
// Copyright 2016, Sander van Harmelen
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

package cloudstack

import (
"encoding/json"
"net/url"
"strconv"
"strings"
)

type CustomServiceParams struct {
p map[string]interface{}
}

func (p *CustomServiceParams) toURLValues() url.Values {
u := url.Values{}
if p.p == nil {
return u
}
for k, v := range p.p {
switch t := v.(type) {
case bool:
u.Set(k, strconv.FormatBool(t))
case int:
u.Set(k, strconv.Itoa(t))
case string:
u.Set(k, t)
case []string:
u.Set(k, strings.Join(t, ", "))
}
}
return u
}

func (p *CustomServiceParams) SetParam(param string, v interface{}) {
if p.p == nil {
p.p = make(map[string]interface{})
}
p.p[param] = v
return
}

func (s *CustomService) CustomRequest(api string, p *CustomServiceParams, result interface{}) error {
resp, err := s.cs.newRequest(api, p.toURLValues())
if err != nil {
return err
}
return json.Unmarshal(resp, result)
}
@@ -80,6 +80,7 @@ type CloudStackClient struct {
CloudIdentifier *CloudIdentifierService
Cluster *ClusterService
Configuration *ConfigurationService
Custom *CustomService
DiskOffering *DiskOfferingService
Domain *DomainService
Event *EventService
@@ -161,6 +162,7 @@ func newClient(apiurl string, apikey string, secret string, async bool, verifyss
cs.CloudIdentifier = NewCloudIdentifierService(cs)
cs.Cluster = NewClusterService(cs)
cs.Configuration = NewConfigurationService(cs)
cs.Custom = NewCustomService(cs)
cs.DiskOffering = NewDiskOfferingService(cs)
cs.Domain = NewDomainService(cs)
cs.Event = NewEventService(cs)
@@ -535,6 +537,14 @@ func NewConfigurationService(cs *CloudStackClient) *ConfigurationService {
return &ConfigurationService{cs: cs}
}

type CustomService struct {
cs *CloudStackClient
}

func NewCustomService(cs *CloudStackClient) *CustomService {
return &CustomService{cs: cs}
}

type DiskOfferingService struct {
cs *CloudStackClient
}
@@ -651,6 +651,47 @@ func (s *service) GenerateCode() ([]byte, error) {
pn("}")
pn("")
}
if s.name == "CustomService" {
pn("type CustomServiceParams struct {")
pn(" p map[string]interface{}")
pn("}")
pn("")
pn("func (p *CustomServiceParams) toURLValues() url.Values {")
pn(" u := url.Values{}")
pn(" if p.p == nil {")
pn(" return u")
pn(" }")
pn(" for k, v := range p.p {")
pn(" switch t := v.(type) {")
pn(" case bool:")
pn(" u.Set(k, strconv.FormatBool(t))")
pn(" case int:")
pn(" u.Set(k, strconv.Itoa(t))")
pn(" case string:")
pn(" u.Set(k, t)")
pn(" case []string:")
pn(" u.Set(k, strings.Join(t, \", \"))")
pn(" }")
pn(" }")
pn(" return u")
pn("}")
pn("")
pn("func (p *CustomServiceParams) SetParam(param string, v interface{}) {")
pn(" if p.p == nil {")
pn(" p.p = make(map[string]interface{})")
pn(" }")
pn(" p.p[param] = v")
pn(" return")
pn("}")
pn("")
pn("func (s *CustomService) CustomRequest(api string, p *CustomServiceParams, result interface{}) error {")
pn(" resp, err := s.cs.newRequest(api, p.toURLValues())")
pn(" if err != nil {")
pn(" return err")
pn(" }")
pn(" return json.Unmarshal(resp, result)")
pn("}")
}

for _, a := range s.apis {
s.generateParamType(a)
@@ -1259,6 +1300,7 @@ func getAllServices() (*allServices, []error, error) {
}
as.services = append(as.services, s)
}
as.services = append(as.services, &service{name: "CustomService"})
sort.Sort(as.services)
return as, errors, nil
}

0 comments on commit 4b447b1

Please sign in to comment.