github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/rts/v1/stacks/results.go (about)

     1  package stacks
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"fmt"
     7  	"io"
     8  	"reflect"
     9  	"strings"
    10  	"time"
    11  
    12  	"github.com/huaweicloud/golangsdk"
    13  	"github.com/huaweicloud/golangsdk/pagination"
    14  )
    15  
    16  // CreatedStack represents the object extracted from a Create operation.
    17  type CreatedStack struct {
    18  	ID    string           `json:"id"`
    19  	Links []golangsdk.Link `json:"links"`
    20  }
    21  
    22  // CreateResult represents the result of a Create operation.
    23  type CreateResult struct {
    24  	golangsdk.Result
    25  }
    26  
    27  // Extract returns a pointer to a CreatedStack object and is called after a
    28  // Create operation.
    29  func (r CreateResult) Extract() (*CreatedStack, error) {
    30  	var s struct {
    31  		CreatedStack *CreatedStack `json:"stack"`
    32  	}
    33  	err := r.ExtractInto(&s)
    34  	return s.CreatedStack, err
    35  }
    36  
    37  // StackPage is a pagination.Pager that is returned from a call to the List function.
    38  type StackPage struct {
    39  	pagination.LinkedPageBase
    40  }
    41  
    42  // IsEmpty returns true if a ListResult contains no Stacks.
    43  func (r StackPage) IsEmpty() (bool, error) {
    44  	stacks, err := ExtractStacks(r)
    45  	return len(stacks) == 0, err
    46  }
    47  
    48  // ListedStack represents an element in the slice extracted from a List operation.
    49  type ListedStack struct {
    50  	CreationTime time.Time        `json:"-"`
    51  	Description  string           `json:"description"`
    52  	ID           string           `json:"id"`
    53  	Links        []golangsdk.Link `json:"links"`
    54  	Name         string           `json:"stack_name"`
    55  	Status       string           `json:"stack_status"`
    56  	StatusReason string           `json:"stack_status_reason"`
    57  	UpdatedTime  time.Time        `json:"-"`
    58  }
    59  
    60  func (r *ListedStack) UnmarshalJSON(b []byte) error {
    61  	type tmp ListedStack
    62  	var s struct {
    63  		tmp
    64  		CreationTime golangsdk.JSONRFC3339NoZ `json:"creation_time"`
    65  		UpdatedTime  golangsdk.JSONRFC3339NoZ `json:"updated_time"`
    66  	}
    67  	err := json.Unmarshal(b, &s)
    68  	if err != nil {
    69  		return err
    70  	}
    71  	*r = ListedStack(s.tmp)
    72  
    73  	r.CreationTime = time.Time(s.CreationTime)
    74  	r.UpdatedTime = time.Time(s.UpdatedTime)
    75  
    76  	return nil
    77  }
    78  
    79  // ExtractStacks extracts and returns a slice of ListedStack. It is used while iterating
    80  // over a stacks.List call.
    81  func ExtractStacks(r pagination.Page) ([]ListedStack, error) {
    82  	var s struct {
    83  		ListedStacks []ListedStack `json:"stacks"`
    84  	}
    85  	err := (r.(StackPage)).ExtractInto(&s)
    86  	return s.ListedStacks, err
    87  }
    88  
    89  // RetrievedStack represents the object extracted from a Get operation.
    90  // RetrievedStack represents the object extracted from a Get operation.
    91  type RetrievedStack struct {
    92  	Capabilities        []interface{}     `json:"capabilities"`
    93  	CreationTime        time.Time         `json:"-"`
    94  	Description         string            `json:"description"`
    95  	DisableRollback     bool              `json:"disable_rollback"`
    96  	ID                  string            `json:"id"`
    97  	TenantId            string            `json:"tenant_id"`
    98  	Links               []golangsdk.Link  `json:"links"`
    99  	NotificationTopics  []interface{}     `json:"notification_topics"`
   100  	Outputs             []*Output         `json:"outputs"`
   101  	Parameters          map[string]string `json:"parameters"`
   102  	Name                string            `json:"stack_name"`
   103  	Status              string            `json:"stack_status"`
   104  	StatusReason        string            `json:"stack_status_reason"`
   105  	Tags                []string          `json:"tags"`
   106  	TemplateDescription string            `json:"template_description"`
   107  	Timeout             int               `json:"timeout_mins"`
   108  	UpdatedTime         time.Time         `json:"-"`
   109  }
   110  
   111  // The Output data type.
   112  type Output struct {
   113  	_ struct{} `type:"structure"`
   114  
   115  	// User defined description associated with the output.
   116  	Description string `json:"description"`
   117  
   118  	// The name of the export associated with the output.
   119  	//ExportName *string `json:"name"`
   120  
   121  	// The key associated with the output.
   122  	OutputKey *string `json:"output_key"`
   123  
   124  	// The value associated with the output.
   125  	OutputValue *string `json:"output_value"`
   126  }
   127  
   128  // String returns the string representation
   129  func (s Output) String() string {
   130  	return Prettify(s)
   131  }
   132  
   133  // GoString returns the string representation
   134  func (s Output) GoString() string {
   135  	return s.String()
   136  }
   137  
   138  // SetDescription sets the Description field's value.
   139  func (s *Output) SetDescription(v string) *Output {
   140  	s.Description = v
   141  	return s
   142  }
   143  
   144  // SetOutputKey sets the OutputKey field's value.
   145  func (s *Output) SetOutputKey(v string) *Output {
   146  	s.OutputKey = &v
   147  	return s
   148  }
   149  
   150  // SetOutputValue sets the OutputValue field's value.
   151  func (s *Output) SetOutputValue(v string) *Output {
   152  	s.OutputValue = &v
   153  	return s
   154  }
   155  
   156  // RetrievedStack represents the object extracted from a Get operation.
   157  func (r *RetrievedStack) UnmarshalJSON(b []byte) error {
   158  	type tmp RetrievedStack
   159  	var s struct {
   160  		tmp
   161  		CreationTime golangsdk.JSONRFC3339NoZ `json:"creation_time"`
   162  		UpdatedTime  golangsdk.JSONRFC3339NoZ `json:"updated_time"`
   163  	}
   164  	err := json.Unmarshal(b, &s)
   165  	if err != nil {
   166  		return err
   167  	}
   168  	*r = RetrievedStack(s.tmp)
   169  
   170  	r.CreationTime = time.Time(s.CreationTime)
   171  	r.UpdatedTime = time.Time(s.UpdatedTime)
   172  
   173  	return nil
   174  }
   175  
   176  // GetResult represents the result of a Get operation.
   177  type GetResult struct {
   178  	golangsdk.Result
   179  }
   180  
   181  // Extract returns a pointer to a CreatedStack object and is called after a
   182  // Create operation.
   183  func (r GetResult) Extract() (*RetrievedStack, error) {
   184  	var s struct {
   185  		Stack *RetrievedStack `json:"stack"`
   186  	}
   187  	err := r.ExtractInto(&s)
   188  	return s.Stack, err
   189  }
   190  
   191  // UpdateResult represents the result of a Update operation.
   192  type UpdateResult struct {
   193  	golangsdk.ErrResult
   194  }
   195  
   196  // DeleteResult represents the result of a Delete operation.
   197  type DeleteResult struct {
   198  	golangsdk.ErrResult
   199  }
   200  
   201  // Prettify returns the string representation of a value.
   202  func Prettify(i interface{}) string {
   203  	var buf bytes.Buffer
   204  	prettify(reflect.ValueOf(i), 0, &buf)
   205  	return buf.String()
   206  }
   207  
   208  // prettify will recursively walk value v to build a textual
   209  // representation of the value.
   210  func prettify(v reflect.Value, indent int, buf *bytes.Buffer) {
   211  	for v.Kind() == reflect.Ptr {
   212  		v = v.Elem()
   213  	}
   214  
   215  	switch v.Kind() {
   216  	case reflect.Struct:
   217  		strtype := v.Type().String()
   218  		if strtype == "time.Time" {
   219  			fmt.Fprintf(buf, "%s", v.Interface())
   220  			break
   221  		} else if strings.HasPrefix(strtype, "io.") {
   222  			buf.WriteString("<buffer>")
   223  			break
   224  		}
   225  
   226  		buf.WriteString("{\n")
   227  
   228  		names := []string{}
   229  		for i := 0; i < v.Type().NumField(); i++ {
   230  			name := v.Type().Field(i).Name
   231  			f := v.Field(i)
   232  			if name[0:1] == strings.ToLower(name[0:1]) {
   233  				continue // ignore unexported fields
   234  			}
   235  			if (f.Kind() == reflect.Ptr || f.Kind() == reflect.Slice || f.Kind() == reflect.Map) && f.IsNil() {
   236  				continue // ignore unset fields
   237  			}
   238  			names = append(names, name)
   239  		}
   240  
   241  		for i, n := range names {
   242  			val := v.FieldByName(n)
   243  			buf.WriteString(strings.Repeat(" ", indent+2))
   244  			buf.WriteString(n + ": ")
   245  			prettify(val, indent+2, buf)
   246  
   247  			if i < len(names)-1 {
   248  				buf.WriteString(",\n")
   249  			}
   250  		}
   251  
   252  		buf.WriteString("\n" + strings.Repeat(" ", indent) + "}")
   253  	case reflect.Slice:
   254  		strtype := v.Type().String()
   255  		if strtype == "[]uint8" {
   256  			fmt.Fprintf(buf, "<binary> len %d", v.Len())
   257  			break
   258  		}
   259  
   260  		nl, id, id2 := "", "", ""
   261  		if v.Len() > 3 {
   262  			nl, id, id2 = "\n", strings.Repeat(" ", indent), strings.Repeat(" ", indent+2)
   263  		}
   264  		buf.WriteString("[" + nl)
   265  		for i := 0; i < v.Len(); i++ {
   266  			buf.WriteString(id2)
   267  			prettify(v.Index(i), indent+2, buf)
   268  
   269  			if i < v.Len()-1 {
   270  				buf.WriteString("," + nl)
   271  			}
   272  		}
   273  
   274  		buf.WriteString(nl + id + "]")
   275  	case reflect.Map:
   276  		buf.WriteString("{\n")
   277  
   278  		for i, k := range v.MapKeys() {
   279  			buf.WriteString(strings.Repeat(" ", indent+2))
   280  			buf.WriteString(k.String() + ": ")
   281  			prettify(v.MapIndex(k), indent+2, buf)
   282  
   283  			if i < v.Len()-1 {
   284  				buf.WriteString(",\n")
   285  			}
   286  		}
   287  
   288  		buf.WriteString("\n" + strings.Repeat(" ", indent) + "}")
   289  	default:
   290  		if !v.IsValid() {
   291  			fmt.Fprint(buf, "<invalid value>")
   292  			return
   293  		}
   294  		format := "%v"
   295  		switch v.Interface().(type) {
   296  		case string:
   297  			format = "%q"
   298  		case io.ReadSeeker, io.Reader:
   299  			format = "buffer(%p)"
   300  		}
   301  		fmt.Fprintf(buf, format, v.Interface())
   302  	}
   303  }