github.com/orange-cloudfoundry/cli@v7.1.0+incompatible/api/cloudcontroller/ccv2/stack.go (about)

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