code.cloudfoundry.org/cli@v7.1.0+incompatible/resources/route_resource.go (about)

     1  package resources
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	"code.cloudfoundry.org/cli/api/cloudcontroller"
     7  )
     8  
     9  type RouteDestinationApp struct {
    10  	GUID    string
    11  	Process struct {
    12  		Type string
    13  	}
    14  }
    15  
    16  type RouteDestination struct {
    17  	GUID string
    18  	App  RouteDestinationApp
    19  }
    20  
    21  type Route struct {
    22  	GUID         string
    23  	SpaceGUID    string
    24  	DomainGUID   string
    25  	Host         string
    26  	Path         string
    27  	Protocol     string
    28  	Port         int
    29  	URL          string
    30  	Destinations []RouteDestination
    31  	Metadata     *Metadata
    32  }
    33  
    34  func (r Route) MarshalJSON() ([]byte, error) {
    35  	type Data struct {
    36  		GUID string `json:"guid,omitempty"`
    37  	}
    38  
    39  	type RelationshipData struct {
    40  		Data Data `json:"data,omitempty"`
    41  	}
    42  
    43  	type Relationships struct {
    44  		Space  RelationshipData `json:"space,omitempty"`
    45  		Domain RelationshipData `json:"domain,omitempty"`
    46  	}
    47  
    48  	// Building up the request body in ccRoute
    49  	type ccRoute struct {
    50  		GUID          string         `json:"guid,omitempty"`
    51  		Host          string         `json:"host,omitempty"`
    52  		Path          string         `json:"path,omitempty"`
    53  		Protocol      string         `json:"protocol,omitempty"`
    54  		Port          int            `json:"port,omitempty"`
    55  		Relationships *Relationships `json:"relationships,omitempty"`
    56  	}
    57  
    58  	ccR := ccRoute{
    59  		GUID:     r.GUID,
    60  		Host:     r.Host,
    61  		Path:     r.Path,
    62  		Protocol: r.Protocol,
    63  		Port:     r.Port,
    64  	}
    65  
    66  	if r.SpaceGUID != "" {
    67  		ccR.Relationships = &Relationships{
    68  			Space:  RelationshipData{Data{GUID: r.SpaceGUID}},
    69  			Domain: RelationshipData{Data{GUID: r.DomainGUID}},
    70  		}
    71  	}
    72  
    73  	return json.Marshal(ccR)
    74  }
    75  
    76  func (r *Route) UnmarshalJSON(data []byte) error {
    77  	var alias struct {
    78  		GUID         string             `json:"guid,omitempty"`
    79  		Protocol     string             `json:"protocol,omitempty"`
    80  		Host         string             `json:"host,omitempty"`
    81  		Path         string             `json:"path,omitempty"`
    82  		Port         int                `json:"port,omitempty"`
    83  		URL          string             `json:"url,omitempty"`
    84  		Destinations []RouteDestination `json:"destinations,omitempty"`
    85  		Metadata     *Metadata          `json:"metadata,omitempty"`
    86  
    87  		Relationships struct {
    88  			Space struct {
    89  				Data struct {
    90  					GUID string `json:"guid,omitempty"`
    91  				} `json:"data,omitempty"`
    92  			} `json:"space,omitempty"`
    93  			Domain struct {
    94  				Data struct {
    95  					GUID string `json:"guid,omitempty"`
    96  				} `json:"data,omitempty"`
    97  			} `json:"domain,omitempty"`
    98  		} `json:"relationships,omitempty"`
    99  	}
   100  
   101  	err := cloudcontroller.DecodeJSON(data, &alias)
   102  	if err != nil {
   103  		return err
   104  	}
   105  
   106  	r.GUID = alias.GUID
   107  	r.Protocol = alias.Protocol
   108  	r.Host = alias.Host
   109  	r.SpaceGUID = alias.Relationships.Space.Data.GUID
   110  	r.DomainGUID = alias.Relationships.Domain.Data.GUID
   111  	r.Path = alias.Path
   112  	r.Port = alias.Port
   113  	r.URL = alias.URL
   114  	r.Destinations = alias.Destinations
   115  	r.Metadata = alias.Metadata
   116  
   117  	return nil
   118  }