github.com/IBM-Cloud/bluemix-go@v0.0.0-20240423071914-9e96525baef4/models/resource_service.go (about)

     1  package models
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	"github.com/IBM-Cloud/bluemix-go/crn"
     7  )
     8  
     9  type Service struct {
    10  	ID         string `json:"id"`
    11  	Name       string `json:"name"`
    12  	CatalogCRN string `json:"catalog_crn"`
    13  	URL        string `json:"url"`
    14  	Kind       string `json:"kind"`
    15  
    16  	Metadata ServiceMetadata `json:"-"`
    17  	Children []Service       `json:"children"`
    18  	Active   bool            `json:"active"`
    19  }
    20  
    21  type ServicePlan struct {
    22  	ID         string `json:"id"`
    23  	Name       string `json:"name"`
    24  	CatalogCRN string `json:"catalog_crn"`
    25  	URL        string `json:"url"`
    26  	Kind       string `json:"kind"`
    27  }
    28  
    29  type ServiceDeployment struct {
    30  	ID         string             `json:"id"`
    31  	Name       string             `json:"name"`
    32  	CatalogCRN string             `json:"catalog_crn"`
    33  	Metadata   DeploymentMetaData `json:"metadata,omitempty"`
    34  }
    35  
    36  type ServiceDeploymentAlias struct {
    37  	Metadata DeploymentMetaData `json:"metadata,omitempty"`
    38  }
    39  
    40  type DeploymentMetaData struct {
    41  	RCCompatible  bool                       `json:"rc_compatible"`
    42  	IAMCompatible bool                       `json:"iam_compatible"`
    43  	Deployment    MetadataDeploymentFragment `json:"deployment,omitempty"`
    44  	Service       MetadataServiceFragment    `json:"service,omitempty"`
    45  }
    46  
    47  type MetadataDeploymentFragment struct {
    48  	DeploymentID string  `json:"deployment_id,omitempty"`
    49  	TargetCrn    crn.CRN `json:"target_crn"`
    50  	Location     string  `json:"location"`
    51  }
    52  
    53  type ServiceMetadata interface{}
    54  
    55  type ServiceResourceMetadata struct {
    56  	Service MetadataServiceFragment `json:"service"`
    57  }
    58  
    59  type MetadataServiceFragment struct {
    60  	Bindable            bool   `json:"bindable"`
    61  	IAMCompatible       bool   `json:"iam_compatible"`
    62  	RCProvisionable     bool   `json:"rc_provisionable"`
    63  	PlanUpdateable      bool   `json:"plan_updateable"`
    64  	ServiceCheckEnabled bool   `json:"service_check_enabled"`
    65  	ServiceKeySupported bool   `json:"service_key_supported"`
    66  	State               string `json:"state"`
    67  	TestCheckInterval   int    `json:"test_check_interval"`
    68  	UniqueAPIKey        bool   `json:"unique_api_key"`
    69  
    70  	// CF properties
    71  	ServiceBrokerGUID string `json:"service_broker_guid"`
    72  }
    73  
    74  type PlatformServiceResourceMetadata struct {
    75  }
    76  
    77  type TemplateResourceMetadata struct {
    78  }
    79  
    80  type RuntimeResourceMetadata struct {
    81  }
    82  
    83  // UnmarshalJSON provide custom JSON unmarshal behavior to support multiple types
    84  // of `metadata`
    85  func (s *Service) UnmarshalJSON(data []byte) error {
    86  	type Copy Service
    87  
    88  	trial := &struct {
    89  		*Copy
    90  		Metadata json.RawMessage `json:"metadata"`
    91  	}{
    92  		Copy: (*Copy)(s),
    93  	}
    94  
    95  	if err := json.Unmarshal(data, trial); err != nil {
    96  		return err
    97  	}
    98  
    99  	if len(trial.Metadata) == 0 {
   100  		s.Metadata = nil
   101  		return nil
   102  	}
   103  
   104  	switch s.Kind {
   105  	case "runtime":
   106  		s.Metadata = &RuntimeResourceMetadata{}
   107  	case "service", "iaas", "composite":
   108  		s.Metadata = &ServiceResourceMetadata{}
   109  	case "platform_service":
   110  		s.Metadata = &PlatformServiceResourceMetadata{}
   111  	case "template":
   112  		s.Metadata = &TemplateResourceMetadata{}
   113  	default:
   114  		s.Metadata = nil
   115  		return nil
   116  	}
   117  
   118  	if err := json.Unmarshal(trial.Metadata, s.Metadata); err != nil {
   119  		return err
   120  	}
   121  	return nil
   122  }