Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions openstack/rts/v1/softwareconfig/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
Package softwareconfig enables management and retrieval of Software Configs

Example to List Software Configs

listOpts := softwareconfig.ListOpts{}
allConfigs, err := softwareconfig.List(client,listOpts)
if err != nil {
panic(err)
}

for _, config := range allConfigs {
fmt.Printf("%+v\n", config)
}

Example to Get Software Deployment

configID:="bd7d48a5-6e33-4b95-aa28-d0d3af46c635"

configs,err:=softwareconfig.Get(client,configID).Extract()

if err != nil {
panic(err)
}


Example to Create a Software Configs

createOpts := softwareconfig.CreateOpts{
Name: "config_test",
}

config, err := softwareconfig.Create(client, createOpts).Extract()
if err != nil {
panic(err)
}

Example to Delete a Software Configs

configID := "8de48948-b6d6-4417-82a5-071f7811af91"
del:=softwareconfig.Delete(client,configID).ExtractErr()
if err != nil {
panic(err)
}
*/
package softwareconfig
133 changes: 133 additions & 0 deletions openstack/rts/v1/softwareconfig/requests.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package softwareconfig

import (
"reflect"

"github.com/huaweicloud/golangsdk"
"github.com/huaweicloud/golangsdk/pagination"
)

// ListOpts allows the filtering and sorting of paginated collections through
// the API. Filtering is achieved by passing in struct field values that map to
// the software config attributes you want to see returned. Marker and Limit are used for pagination.
type ListOpts struct {
Id string
Name string
Marker string `q:"marker"`
Limit int `q:"limit"`
}

// List returns collection of
// Software Config. It accepts a ListOpts struct, which allows you to filter and sort
// the returned collection for greater efficiency.
//
// Default policy settings return only those Software Config that are owned by the
// tenant who submits the request, unless an admin user submits the request.
func List(c *golangsdk.ServiceClient, opts ListOpts) ([]SoftwareConfig, error) {
q, err := golangsdk.BuildQueryString(&opts)
if err != nil {
return nil, err
}
u := rootURL(c) + q.String()
pages, err := pagination.NewPager(c, u, func(r pagination.PageResult) pagination.Page {
return SoftwareConfigPage{pagination.LinkedPageBase{PageResult: r}}
}).AllPages()

allConfigs, err := ExtractSoftwareConfigs(pages)
if err != nil {
return nil, err
}

return FilterSoftwareConfig(allConfigs, opts)
}

func FilterSoftwareConfig(config []SoftwareConfig, opts ListOpts) ([]SoftwareConfig, error) {

var refinedSoftwareConfig []SoftwareConfig
var matched bool
m := map[string]interface{}{}

if opts.Id != "" {
m["Id"] = opts.Id
}
if opts.Name != "" {
m["Name"] = opts.Name
}

if len(m) > 0 && len(config) > 0 {
for _, config := range config {
matched = true

for key, value := range m {
if sVal := getStructField(&config, key); !(sVal == value) {
matched = false
}
}

if matched {
refinedSoftwareConfig = append(refinedSoftwareConfig, config)
}
}
} else {
refinedSoftwareConfig = config
}
return refinedSoftwareConfig, nil
}

func getStructField(v *SoftwareConfig, field string) string {
r := reflect.ValueOf(v)
f := reflect.Indirect(r).FieldByName(field)
return string(f.String())
}

// CreateOptsBuilder allows extensions to add additional parameters to the
// Create request.
type CreateOptsBuilder interface {
ToSoftwareConfigCreateMap() (map[string]interface{}, error)
}

// CreateOpts contains all the values needed to create a new Software Config. There are
// no required values.
type CreateOpts struct {
// Specifies the script used for defining the configuration.
Config string `json:"config,omitempty"`
//Specifies the name of the software configuration group.
Group string `json:"group,omitempty"`
//Specifies the name of the software configuration.
Name string `json:"name" required:"true"`
//Specifies the software configuration input.
Inputs []map[string]interface{} `json:"inputs,omitempty"`
//Specifies the software configuration output.
Outputs []map[string]interface{} `json:"outputs,omitempty"`
//Specifies options used by a software configuration management tool.
Options map[string]interface{} `json:"options,omitempty"`
}

// ToSoftwareConfigCreateMap builds a create request body from CreateOpts.
func (opts CreateOpts) ToSoftwareConfigCreateMap() (map[string]interface{}, error) {
return golangsdk.BuildRequestBody(opts, "")
}

// Create accepts a CreateOpts struct and uses the values to create a new Software config
func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
b, err := opts.ToSoftwareConfigCreateMap()
if err != nil {
r.Err = err
return
}
reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200}}
_, r.Err = c.Post(rootURL(c), b, &r.Body, reqOpt)
return
}

// Get retrieves a particular software config based on its unique ID.
func Get(c *golangsdk.ServiceClient, id string) (r GetResult) {
_, r.Err = c.Get(resourceURL(c, id), &r.Body, nil)
return
}

// Delete will permanently delete a particular Software Config based on its unique ID.
func Delete(c *golangsdk.ServiceClient, id string) (r DeleteResult) {
_, r.Err = c.Delete(resourceURL(c, id), nil)
return
}
99 changes: 99 additions & 0 deletions openstack/rts/v1/softwareconfig/results.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package softwareconfig

import (
"github.com/huaweicloud/golangsdk"
"github.com/huaweicloud/golangsdk/pagination"
)

type SoftwareConfig struct {
// Specifies the software configuration input.
Inputs []map[string]interface{} `json:"inputs"`
//Specifies the name of the software configuration.
Name string `json:"name"`
//Specifies the software configuration output.
Outputs []map[string]interface{} `json:"outputs"`
//Specifies the time when a configuration is created.
CreationTime golangsdk.JSONRFC3339NoZ `json:"creation_time"`
//Specifies the name of the software configuration group.
Group string `json:"group"`
//Specifies the configuration code.
Config string `json:"config"`
//Specifies configuration options.
Options map[string]interface{} `json:"options"`
//Specifies the software configuration ID.
Id string `json:"id"`
}

// SoftwareConfigPage is the page returned by a pager when traversing over a
// collection of Software Configurations.
type SoftwareConfigPage struct {
pagination.LinkedPageBase
}

// NextPageURL is invoked when a paginated collection of Software Configs has reached
// the end of a page and the pager seeks to traverse over a new one. In order
// to do this, it needs to construct the next page's URL.
func (r SoftwareConfigPage) NextPageURL() (string, error) {
var s struct {
Links []golangsdk.Link `json:"software_config_links"`
}
err := r.ExtractInto(&s)
if err != nil {
return "", err
}
return golangsdk.ExtractNextURL(s.Links)
}

// IsEmpty checks whether a SoftwareConfigPage struct is empty.
func (r SoftwareConfigPage) IsEmpty() (bool, error) {
is, err := ExtractSoftwareConfigs(r)
return len(is) == 0, err
}

// ExtractSoftwareConfigs accepts a Page struct, specifically a SoftwareConfigPage struct,
// and extracts the elements into a slice of Software Configs structs. In other words,
// a generic collection is mapped into a relevant slice.
func ExtractSoftwareConfigs(r pagination.Page) ([]SoftwareConfig, error) {
var s struct {
SoftwareConfigs []SoftwareConfig `json:"software_configs"`
}
err := (r.(SoftwareConfigPage)).ExtractInto(&s)
return s.SoftwareConfigs, err
}

type commonResult struct {
golangsdk.Result
}

// Extract is a function that accepts a result and extracts a Software configuration.
func (r commonResult) Extract() (*SoftwareConfig, error) {
var s struct {
SoftwareConfig *SoftwareConfig `json:"software_config"`
}
err := r.ExtractInto(&s)
return s.SoftwareConfig, err
}

// CreateResult represents the result of a create operation. Call its Extract
// method to interpret it as a Software configuration.
type CreateResult struct {
commonResult
}

// GetResult represents the result of a get operation. Call its Extract
// method to interpret it as a Software configuration.
type GetResult struct {
commonResult
}

// UpdateResult represents the result of an update operation. Call its Extract
// method to interpret it as a Software configuration.
type UpdateResult struct {
commonResult
}

// DeleteResult represents the result of a delete operation. Call its ExtractErr
// method to determine if the request succeeded or failed.
type DeleteResult struct {
golangsdk.ErrResult
}
Loading