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

     1  package ccv3
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  
     7  	"code.cloudfoundry.org/cli/api/cloudcontroller"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     9  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/internal"
    10  )
    11  
    12  // IsolationSegment represents a Cloud Controller Isolation Segment.
    13  type IsolationSegment struct {
    14  	Name string `json:"name"`
    15  	GUID string `json:"guid,omitempty"`
    16  }
    17  
    18  // CreateIsolationSegment will create an Isolation Segment on the Cloud
    19  // Controller. Note: This will not validate that the placement tag exists in
    20  // the diego cluster.
    21  func (client *Client) CreateIsolationSegment(isolationSegment IsolationSegment) (IsolationSegment, Warnings, error) {
    22  	body, err := json.Marshal(isolationSegment)
    23  	if err != nil {
    24  		return IsolationSegment{}, nil, err
    25  	}
    26  
    27  	request, err := client.newHTTPRequest(requestOptions{
    28  		RequestName: internal.PostIsolationSegmentsRequest,
    29  		Body:        bytes.NewReader(body),
    30  	})
    31  	if err != nil {
    32  		return IsolationSegment{}, nil, err
    33  	}
    34  
    35  	var responseIsolationSegment IsolationSegment
    36  	response := cloudcontroller.Response{
    37  		Result: &responseIsolationSegment,
    38  	}
    39  
    40  	err = client.connection.Make(request, &response)
    41  	return responseIsolationSegment, response.Warnings, err
    42  }
    43  
    44  // GetIsolationSegments lists isolation segments with optional filters.
    45  func (client *Client) GetIsolationSegments(query ...Query) ([]IsolationSegment, Warnings, error) {
    46  	request, err := client.newHTTPRequest(requestOptions{
    47  		RequestName: internal.GetIsolationSegmentsRequest,
    48  		Query:       query,
    49  	})
    50  	if err != nil {
    51  		return nil, nil, err
    52  	}
    53  
    54  	var fullIsolationSegmentsList []IsolationSegment
    55  	warnings, err := client.paginate(request, IsolationSegment{}, func(item interface{}) error {
    56  		if isolationSegment, ok := item.(IsolationSegment); ok {
    57  			fullIsolationSegmentsList = append(fullIsolationSegmentsList, isolationSegment)
    58  		} else {
    59  			return ccerror.UnknownObjectInListError{
    60  				Expected:   IsolationSegment{},
    61  				Unexpected: item,
    62  			}
    63  		}
    64  		return nil
    65  	})
    66  
    67  	return fullIsolationSegmentsList, warnings, err
    68  }
    69  
    70  // GetIsolationSegment returns back the requested isolation segment that
    71  // matches the GUID.
    72  func (client *Client) GetIsolationSegment(guid string) (IsolationSegment, Warnings, error) {
    73  	request, err := client.newHTTPRequest(requestOptions{
    74  		RequestName: internal.GetIsolationSegmentRequest,
    75  		URIParams:   map[string]string{"isolation_segment_guid": guid},
    76  	})
    77  	if err != nil {
    78  		return IsolationSegment{}, nil, err
    79  	}
    80  	var isolationSegment IsolationSegment
    81  	response := cloudcontroller.Response{
    82  		Result: &isolationSegment,
    83  	}
    84  
    85  	err = client.connection.Make(request, &response)
    86  	if err != nil {
    87  		return IsolationSegment{}, response.Warnings, err
    88  	}
    89  
    90  	return isolationSegment, response.Warnings, nil
    91  }
    92  
    93  // DeleteIsolationSegment removes an isolation segment from the cloud
    94  // controller. Note: This will only remove it from the cloud controller
    95  // database. It will not remove it from diego.
    96  func (client *Client) DeleteIsolationSegment(guid string) (Warnings, error) {
    97  	request, err := client.newHTTPRequest(requestOptions{
    98  		RequestName: internal.DeleteIsolationSegmentRequest,
    99  		URIParams:   map[string]string{"isolation_segment_guid": guid},
   100  	})
   101  	if err != nil {
   102  		return nil, err
   103  	}
   104  
   105  	var response cloudcontroller.Response
   106  	err = client.connection.Make(request, &response)
   107  	return response.Warnings, err
   108  }