github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/api/cloudcontroller/ccv2/stack.go (about)

     1  package ccv2
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	"code.cloudfoundry.org/cli/api/cloudcontroller"
     7  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/internal"
     9  )
    10  
    11  // Stack represents a Cloud Controller Stack.
    12  type Stack struct {
    13  	GUID        string
    14  	Name        string
    15  	Description string
    16  }
    17  
    18  // UnmarshalJSON helps unmarshal a Cloud Controller Stack response.
    19  func (stack *Stack) UnmarshalJSON(data []byte) error {
    20  	var ccStack struct {
    21  		Metadata internal.Metadata `json:"metadata"`
    22  		Entity   struct {
    23  			Name        string `json:"name"`
    24  			Description string `json:"description"`
    25  		} `json:"entity"`
    26  	}
    27  	if err := json.Unmarshal(data, &ccStack); err != nil {
    28  		return err
    29  	}
    30  
    31  	stack.GUID = ccStack.Metadata.GUID
    32  	stack.Name = ccStack.Entity.Name
    33  	stack.Description = ccStack.Entity.Description
    34  	return nil
    35  }
    36  
    37  // GetStack returns the requested stack.
    38  func (client *Client) GetStack(guid string) (Stack, Warnings, error) {
    39  	request, err := client.newHTTPRequest(requestOptions{
    40  		RequestName: internal.GetStackRequest,
    41  		URIParams:   Params{"stack_guid": guid},
    42  	})
    43  	if err != nil {
    44  		return Stack{}, nil, err
    45  	}
    46  
    47  	var stack Stack
    48  	response := cloudcontroller.Response{
    49  		Result: &stack,
    50  	}
    51  
    52  	err = client.connection.Make(request, &response)
    53  	return stack, response.Warnings, err
    54  }
    55  
    56  // GetStacks returns a list of Stacks based off of the provided filters.
    57  func (client *Client) GetStacks(filters ...Filter) ([]Stack, Warnings, error) {
    58  	request, err := client.newHTTPRequest(requestOptions{
    59  		RequestName: internal.GetStacksRequest,
    60  		Query:       ConvertFilterParameters(filters),
    61  	})
    62  	if err != nil {
    63  		return nil, nil, err
    64  	}
    65  
    66  	var fullStacksList []Stack
    67  	warnings, err := client.paginate(request, Stack{}, func(item interface{}) error {
    68  		if space, ok := item.(Stack); ok {
    69  			fullStacksList = append(fullStacksList, space)
    70  		} else {
    71  			return ccerror.UnknownObjectInListError{
    72  				Expected:   Stack{},
    73  				Unexpected: item,
    74  			}
    75  		}
    76  		return nil
    77  	})
    78  
    79  	return fullStacksList, warnings, err
    80  }