github.com/DaAlbrecht/cf-cli@v0.0.0-20231128151943-1fe19bb400b9/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  	Port     int
    20  	Protocol string
    21  }
    22  
    23  type Route struct {
    24  	GUID         string
    25  	SpaceGUID    string
    26  	DomainGUID   string
    27  	Host         string
    28  	Path         string
    29  	Protocol     string
    30  	Port         int
    31  	URL          string
    32  	Destinations []RouteDestination
    33  	Metadata     *Metadata
    34  }
    35  
    36  func (r Route) MarshalJSON() ([]byte, error) {
    37  	type Data struct {
    38  		GUID string `json:"guid,omitempty"`
    39  	}
    40  
    41  	type RelationshipData struct {
    42  		Data Data `json:"data,omitempty"`
    43  	}
    44  
    45  	type Relationships struct {
    46  		Space  RelationshipData `json:"space,omitempty"`
    47  		Domain RelationshipData `json:"domain,omitempty"`
    48  	}
    49  
    50  	// Building up the request body in ccRoute
    51  	type ccRoute struct {
    52  		GUID          string         `json:"guid,omitempty"`
    53  		Host          string         `json:"host,omitempty"`
    54  		Path          string         `json:"path,omitempty"`
    55  		Protocol      string         `json:"protocol,omitempty"`
    56  		Port          int            `json:"port,omitempty"`
    57  		Relationships *Relationships `json:"relationships,omitempty"`
    58  	}
    59  
    60  	ccR := ccRoute{
    61  		GUID:     r.GUID,
    62  		Host:     r.Host,
    63  		Path:     r.Path,
    64  		Protocol: r.Protocol,
    65  		Port:     r.Port,
    66  	}
    67  
    68  	if r.SpaceGUID != "" {
    69  		ccR.Relationships = &Relationships{
    70  			Space:  RelationshipData{Data{GUID: r.SpaceGUID}},
    71  			Domain: RelationshipData{Data{GUID: r.DomainGUID}},
    72  		}
    73  	}
    74  
    75  	return json.Marshal(ccR)
    76  }
    77  
    78  func (r *Route) UnmarshalJSON(data []byte) error {
    79  	var alias struct {
    80  		GUID         string             `json:"guid,omitempty"`
    81  		Protocol     string             `json:"protocol,omitempty"`
    82  		Host         string             `json:"host,omitempty"`
    83  		Path         string             `json:"path,omitempty"`
    84  		Port         int                `json:"port,omitempty"`
    85  		URL          string             `json:"url,omitempty"`
    86  		Destinations []RouteDestination `json:"destinations,omitempty"`
    87  		Metadata     *Metadata          `json:"metadata,omitempty"`
    88  
    89  		Relationships struct {
    90  			Space struct {
    91  				Data struct {
    92  					GUID string `json:"guid,omitempty"`
    93  				} `json:"data,omitempty"`
    94  			} `json:"space,omitempty"`
    95  			Domain struct {
    96  				Data struct {
    97  					GUID string `json:"guid,omitempty"`
    98  				} `json:"data,omitempty"`
    99  			} `json:"domain,omitempty"`
   100  		} `json:"relationships,omitempty"`
   101  	}
   102  
   103  	err := cloudcontroller.DecodeJSON(data, &alias)
   104  	if err != nil {
   105  		return err
   106  	}
   107  
   108  	r.GUID = alias.GUID
   109  	r.Protocol = alias.Protocol
   110  	r.Host = alias.Host
   111  	r.SpaceGUID = alias.Relationships.Space.Data.GUID
   112  	r.DomainGUID = alias.Relationships.Domain.Data.GUID
   113  	r.Path = alias.Path
   114  	r.Port = alias.Port
   115  	r.URL = alias.URL
   116  	r.Destinations = alias.Destinations
   117  	r.Metadata = alias.Metadata
   118  
   119  	return nil
   120  }