github.com/cloudbase/juju-core@v0.0.0-20140504232958-a7271ac7912f/state/api/params/params.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package params
     5  
     6  import (
     7  	"bytes"
     8  	"encoding/json"
     9  	"fmt"
    10  
    11  	"launchpad.net/juju-core/charm"
    12  	"launchpad.net/juju-core/constraints"
    13  	"launchpad.net/juju-core/instance"
    14  	"launchpad.net/juju-core/juju/osenv"
    15  	"launchpad.net/juju-core/utils/ssh"
    16  	"launchpad.net/juju-core/version"
    17  )
    18  
    19  // ErrorResults holds the results of calling a bulk operation which
    20  // returns no data, only an error result. The order and
    21  // number of elements matches the operations specified in the request.
    22  type ErrorResults struct {
    23  	// Results contains the error results from each operation.
    24  	Results []ErrorResult
    25  }
    26  
    27  // OneError returns the error from the result
    28  // of a bulk operation on a single value.
    29  func (result ErrorResults) OneError() error {
    30  	if n := len(result.Results); n != 1 {
    31  		return fmt.Errorf("expected one result, got %d", n)
    32  	}
    33  	if err := result.Results[0].Error; err != nil {
    34  		return err
    35  	}
    36  	return nil
    37  }
    38  
    39  // ErrorResult holds the error status of a single operation.
    40  type ErrorResult struct {
    41  	Error *Error
    42  }
    43  
    44  // StatusData contains additional information for a status.
    45  type StatusData map[string]interface{}
    46  
    47  // AddRelation holds the parameters for making the AddRelation call.
    48  // The endpoints specified are unordered.
    49  type AddRelation struct {
    50  	Endpoints []string
    51  }
    52  
    53  // AddRelationResults holds the results of a AddRelation call. The Endpoints
    54  // field maps service names to the involved endpoints.
    55  type AddRelationResults struct {
    56  	Endpoints map[string]charm.Relation
    57  }
    58  
    59  // DestroyRelation holds the parameters for making the DestroyRelation call.
    60  // The endpoints specified are unordered.
    61  type DestroyRelation struct {
    62  	Endpoints []string
    63  }
    64  
    65  // AddMachineParams encapsulates the parameters used to create a new machine.
    66  type AddMachineParams struct {
    67  	// The following fields hold attributes that will be given to the
    68  	// new machine when it is created.
    69  	Series      string
    70  	Constraints constraints.Value
    71  	Jobs        []MachineJob
    72  
    73  	// If ParentId is non-empty, it specifies the id of the
    74  	// parent machine within which the new machine will
    75  	// be created. In that case, ContainerType must also be
    76  	// set.
    77  	ParentId string
    78  
    79  	// ContainerType optionally gives the container type of the
    80  	// new machine. If it is non-empty, the new machine
    81  	// will be implemented by a container. If it is specified
    82  	// but ParentId is empty, a new top level machine will
    83  	// be created to hold the container with given series,
    84  	// constraints and jobs.
    85  	ContainerType instance.ContainerType
    86  
    87  	// If InstanceId is non-empty, it will be associated with
    88  	// the new machine along with the given nonce,
    89  	// hardware characteristics and addresses.
    90  	// All the following fields will be ignored if ContainerType
    91  	// is set.
    92  	InstanceId              instance.Id
    93  	Nonce                   string
    94  	HardwareCharacteristics instance.HardwareCharacteristics
    95  	Addrs                   []instance.Address
    96  }
    97  
    98  // AddMachines holds the parameters for making the AddMachines call.
    99  type AddMachines struct {
   100  	MachineParams []AddMachineParams
   101  }
   102  
   103  // AddMachinesResults holds the results of an AddMachines call.
   104  type AddMachinesResults struct {
   105  	Machines []AddMachinesResult
   106  }
   107  
   108  // AddMachinesResults holds the name of a machine added by the
   109  // state.api.client.AddMachine call for a single machine.
   110  type AddMachinesResult struct {
   111  	Machine string
   112  	Error   *Error
   113  }
   114  
   115  // DestroyMachines holds parameters for the DestroyMachines call.
   116  type DestroyMachines struct {
   117  	MachineNames []string
   118  	Force        bool
   119  }
   120  
   121  // ServiceDeploy holds the parameters for making the ServiceDeploy call.
   122  type ServiceDeploy struct {
   123  	ServiceName   string
   124  	CharmUrl      string
   125  	NumUnits      int
   126  	Config        map[string]string
   127  	ConfigYAML    string // Takes precedence over config if both are present.
   128  	Constraints   constraints.Value
   129  	ToMachineSpec string
   130  }
   131  
   132  // ServiceUpdate holds the parameters for making the ServiceUpdate call.
   133  type ServiceUpdate struct {
   134  	ServiceName     string
   135  	CharmUrl        string
   136  	ForceCharmUrl   bool
   137  	MinUnits        *int
   138  	SettingsStrings map[string]string
   139  	SettingsYAML    string // Takes precedence over SettingsStrings if both are present.
   140  	Constraints     *constraints.Value
   141  }
   142  
   143  // ServiceSetCharm sets the charm for a given service.
   144  type ServiceSetCharm struct {
   145  	ServiceName string
   146  	CharmUrl    string
   147  	Force       bool
   148  }
   149  
   150  // ServiceExpose holds the parameters for making the ServiceExpose call.
   151  type ServiceExpose struct {
   152  	ServiceName string
   153  }
   154  
   155  // ServiceSet holds the parameters for a ServiceSet
   156  // command. Options contains the configuration data.
   157  type ServiceSet struct {
   158  	ServiceName string
   159  	Options     map[string]string
   160  }
   161  
   162  // ServiceSetYAML holds the parameters for
   163  // a ServiceSetYAML command. Config contains the
   164  // configuration data in YAML format.
   165  type ServiceSetYAML struct {
   166  	ServiceName string
   167  	Config      string
   168  }
   169  
   170  // ServiceUnset holds the parameters for a ServiceUnset
   171  // command. Options contains the option attribute names
   172  // to unset.
   173  type ServiceUnset struct {
   174  	ServiceName string
   175  	Options     []string
   176  }
   177  
   178  // ServiceGet holds parameters for making the ServiceGet or
   179  // ServiceGetCharmURL calls.
   180  type ServiceGet struct {
   181  	ServiceName string
   182  }
   183  
   184  // ServiceGetResults holds results of the ServiceGet call.
   185  type ServiceGetResults struct {
   186  	Service     string
   187  	Charm       string
   188  	Config      map[string]interface{}
   189  	Constraints constraints.Value
   190  }
   191  
   192  // ServiceCharmRelations holds parameters for making the ServiceCharmRelations call.
   193  type ServiceCharmRelations struct {
   194  	ServiceName string
   195  }
   196  
   197  // ServiceCharmRelationsResults holds the results of the ServiceCharmRelations call.
   198  type ServiceCharmRelationsResults struct {
   199  	CharmRelations []string
   200  }
   201  
   202  // ServiceUnexpose holds parameters for the ServiceUnexpose call.
   203  type ServiceUnexpose struct {
   204  	ServiceName string
   205  }
   206  
   207  // PublicAddress holds parameters for the PublicAddress call.
   208  type PublicAddress struct {
   209  	Target string
   210  }
   211  
   212  // PublicAddressResults holds results of the PublicAddress call.
   213  type PublicAddressResults struct {
   214  	PublicAddress string
   215  }
   216  
   217  // Resolved holds parameters for the Resolved call.
   218  type Resolved struct {
   219  	UnitName string
   220  	Retry    bool
   221  }
   222  
   223  // ResolvedResults holds results of the Resolved call.
   224  type ResolvedResults struct {
   225  	Service  string
   226  	Charm    string
   227  	Settings map[string]interface{}
   228  }
   229  
   230  // AddServiceUnitsResults holds the names of the units added by the
   231  // AddServiceUnits call.
   232  type AddServiceUnitsResults struct {
   233  	Units []string
   234  }
   235  
   236  // AddServiceUnits holds parameters for the AddUnits call.
   237  type AddServiceUnits struct {
   238  	ServiceName   string
   239  	NumUnits      int
   240  	ToMachineSpec string
   241  }
   242  
   243  // DestroyServiceUnits holds parameters for the DestroyUnits call.
   244  type DestroyServiceUnits struct {
   245  	UnitNames []string
   246  }
   247  
   248  // ServiceDestroy holds the parameters for making the ServiceDestroy call.
   249  type ServiceDestroy struct {
   250  	ServiceName string
   251  }
   252  
   253  // Creds holds credentials for identifying an entity.
   254  type Creds struct {
   255  	AuthTag  string
   256  	Password string
   257  	Nonce    string
   258  }
   259  
   260  // GetAnnotationsResults holds annotations associated with an entity.
   261  type GetAnnotationsResults struct {
   262  	Annotations map[string]string
   263  }
   264  
   265  // GetAnnotations stores parameters for making the GetAnnotations call.
   266  type GetAnnotations struct {
   267  	Tag string
   268  }
   269  
   270  // SetAnnotations stores parameters for making the SetAnnotations call.
   271  type SetAnnotations struct {
   272  	Tag   string
   273  	Pairs map[string]string
   274  }
   275  
   276  // GetServiceConstraints stores parameters for making the GetServiceConstraints call.
   277  type GetServiceConstraints struct {
   278  	ServiceName string
   279  }
   280  
   281  // GetConstraintsResults holds results of the GetConstraints call.
   282  type GetConstraintsResults struct {
   283  	Constraints constraints.Value
   284  }
   285  
   286  // SetConstraints stores parameters for making the SetConstraints call.
   287  type SetConstraints struct {
   288  	ServiceName string //optional, if empty, environment constraints are set.
   289  	Constraints constraints.Value
   290  }
   291  
   292  // CharmInfo stores parameters for a CharmInfo call.
   293  type CharmInfo struct {
   294  	CharmURL string
   295  }
   296  
   297  // AllWatcherId holds the id of an AllWatcher.
   298  type AllWatcherId struct {
   299  	AllWatcherId string
   300  }
   301  
   302  // AllWatcherNextResults holds deltas returned from calling AllWatcher.Next().
   303  type AllWatcherNextResults struct {
   304  	Deltas []Delta
   305  }
   306  
   307  // Delta holds details of a change to the environment.
   308  type Delta struct {
   309  	// If Removed is true, the entity has been removed;
   310  	// otherwise it has been created or changed.
   311  	Removed bool
   312  	// Entity holds data about the entity that has changed.
   313  	Entity EntityInfo
   314  }
   315  
   316  // ListSSHKeys stores parameters used for a KeyManager.ListKeys call.
   317  type ListSSHKeys struct {
   318  	Entities
   319  	Mode ssh.ListMode
   320  }
   321  
   322  // ModifySSHKeys stores parameters used for a KeyManager.Add|Delete|Import call for a user.
   323  type ModifyUserSSHKeys struct {
   324  	User string
   325  	Keys []string
   326  }
   327  
   328  // MarshalJSON implements json.Marshaler.
   329  func (d *Delta) MarshalJSON() ([]byte, error) {
   330  	b, err := json.Marshal(d.Entity)
   331  	if err != nil {
   332  		return nil, err
   333  	}
   334  	var buf bytes.Buffer
   335  	buf.WriteByte('[')
   336  	c := "change"
   337  	if d.Removed {
   338  		c = "remove"
   339  	}
   340  	fmt.Fprintf(&buf, "%q,%q,", d.Entity.EntityId().Kind, c)
   341  	buf.Write(b)
   342  	buf.WriteByte(']')
   343  	return buf.Bytes(), nil
   344  }
   345  
   346  // UnmarshalJSON implements json.Unmarshaler.
   347  func (d *Delta) UnmarshalJSON(data []byte) error {
   348  	var elements []json.RawMessage
   349  	if err := json.Unmarshal(data, &elements); err != nil {
   350  		return err
   351  	}
   352  	if len(elements) != 3 {
   353  		return fmt.Errorf(
   354  			"Expected 3 elements in top-level of JSON but got %d",
   355  			len(elements))
   356  	}
   357  	var entityKind, operation string
   358  	if err := json.Unmarshal(elements[0], &entityKind); err != nil {
   359  		return err
   360  	}
   361  	if err := json.Unmarshal(elements[1], &operation); err != nil {
   362  		return err
   363  	}
   364  	if operation == "remove" {
   365  		d.Removed = true
   366  	} else if operation != "change" {
   367  		return fmt.Errorf("Unexpected operation %q", operation)
   368  	}
   369  	switch entityKind {
   370  	case "machine":
   371  		d.Entity = new(MachineInfo)
   372  	case "service":
   373  		d.Entity = new(ServiceInfo)
   374  	case "unit":
   375  		d.Entity = new(UnitInfo)
   376  	case "relation":
   377  		d.Entity = new(RelationInfo)
   378  	case "annotation":
   379  		d.Entity = new(AnnotationInfo)
   380  	default:
   381  		return fmt.Errorf("Unexpected entity name %q", entityKind)
   382  	}
   383  	if err := json.Unmarshal(elements[2], &d.Entity); err != nil {
   384  		return err
   385  	}
   386  	return nil
   387  }
   388  
   389  // EntityInfo is implemented by all entity Info types.
   390  type EntityInfo interface {
   391  	// EntityId returns an identifier that will uniquely
   392  	// identify the entity within its kind
   393  	EntityId() EntityId
   394  }
   395  
   396  // IMPORTANT NOTE: the types below are direct subsets of the entity docs
   397  // held in mongo, as defined in the state package (serviceDoc,
   398  // machineDoc etc).
   399  // In particular, the document marshalled into mongo
   400  // must unmarshal correctly into these documents.
   401  // If the format of a field in a document is changed in mongo, or
   402  // a field is removed and it coincides with one of the
   403  // fields below, a similar change must be made here.
   404  //
   405  // MachineInfo corresponds with state.machineDoc.
   406  // ServiceInfo corresponds with state.serviceDoc.
   407  // UnitInfo corresponds with state.unitDoc.
   408  // RelationInfo corresponds with state.relationDoc.
   409  // AnnotationInfo corresponds with state.annotatorDoc.
   410  
   411  var (
   412  	_ EntityInfo = (*MachineInfo)(nil)
   413  	_ EntityInfo = (*ServiceInfo)(nil)
   414  	_ EntityInfo = (*UnitInfo)(nil)
   415  	_ EntityInfo = (*RelationInfo)(nil)
   416  	_ EntityInfo = (*AnnotationInfo)(nil)
   417  )
   418  
   419  type EntityId struct {
   420  	Kind string
   421  	Id   interface{}
   422  }
   423  
   424  // MachineInfo holds the information about a Machine
   425  // that is watched by StateWatcher.
   426  type MachineInfo struct {
   427  	Id         string `bson:"_id"`
   428  	InstanceId string
   429  	Status     Status
   430  	StatusInfo string
   431  	StatusData StatusData
   432  }
   433  
   434  func (i *MachineInfo) EntityId() EntityId {
   435  	return EntityId{
   436  		Kind: "machine",
   437  		Id:   i.Id,
   438  	}
   439  }
   440  
   441  type ServiceInfo struct {
   442  	Name        string `bson:"_id"`
   443  	Exposed     bool
   444  	CharmURL    string
   445  	OwnerTag    string
   446  	Life        Life
   447  	MinUnits    int
   448  	Constraints constraints.Value
   449  	Config      map[string]interface{}
   450  }
   451  
   452  func (i *ServiceInfo) EntityId() EntityId {
   453  	return EntityId{
   454  		Kind: "service",
   455  		Id:   i.Name,
   456  	}
   457  }
   458  
   459  type UnitInfo struct {
   460  	Name           string `bson:"_id"`
   461  	Service        string
   462  	Series         string
   463  	CharmURL       string
   464  	PublicAddress  string
   465  	PrivateAddress string
   466  	MachineId      string
   467  	Ports          []instance.Port
   468  	Status         Status
   469  	StatusInfo     string
   470  	StatusData     StatusData
   471  }
   472  
   473  func (i *UnitInfo) EntityId() EntityId {
   474  	return EntityId{
   475  		Kind: "unit",
   476  		Id:   i.Name,
   477  	}
   478  }
   479  
   480  type Endpoint struct {
   481  	ServiceName string
   482  	Relation    charm.Relation
   483  }
   484  
   485  type RelationInfo struct {
   486  	Key       string `bson:"_id"`
   487  	Id        int
   488  	Endpoints []Endpoint
   489  }
   490  
   491  func (i *RelationInfo) EntityId() EntityId {
   492  	return EntityId{
   493  		Kind: "relation",
   494  		Id:   i.Key,
   495  	}
   496  }
   497  
   498  type AnnotationInfo struct {
   499  	Tag         string
   500  	Annotations map[string]string
   501  }
   502  
   503  func (i *AnnotationInfo) EntityId() EntityId {
   504  	return EntityId{
   505  		Kind: "annotation",
   506  		Id:   i.Tag,
   507  	}
   508  }
   509  
   510  // ContainerConfig contains information from the environment config that is
   511  // needed for container cloud-init.
   512  type ContainerConfig struct {
   513  	ProviderType            string
   514  	AuthorizedKeys          string
   515  	SSLHostnameVerification bool
   516  	Proxy                   osenv.ProxySettings
   517  	AptProxy                osenv.ProxySettings
   518  }
   519  
   520  // ProvisioningScriptParams contains the parameters for the
   521  // ProvisioningScript client API call.
   522  type ProvisioningScriptParams struct {
   523  	MachineId string
   524  	Nonce     string
   525  
   526  	// DataDir may be "", in which case the default will be used.
   527  	DataDir string
   528  
   529  	// DisablePackageCommands may be set to disable all package-related
   530  	// commands. It is then the responsibility of the provisioner to
   531  	// ensure that all the packages required by Juju are available.
   532  	DisablePackageCommands bool
   533  }
   534  
   535  // ProvisioningScriptResult contains the result of the
   536  // ProvisioningScript client API call.
   537  type ProvisioningScriptResult struct {
   538  	Script string
   539  }
   540  
   541  // EnvironmentGetResults contains the result of EnvironmentGet client
   542  // API call.
   543  type EnvironmentGetResults struct {
   544  	Config map[string]interface{}
   545  }
   546  
   547  // EnvironmentSet contains the arguments for EnvironmentSet client API
   548  // call.
   549  type EnvironmentSet struct {
   550  	Config map[string]interface{}
   551  }
   552  
   553  // SetEnvironAgentVersion contains the arguments for
   554  // SetEnvironAgentVersion client API call.
   555  type SetEnvironAgentVersion struct {
   556  	Version version.Number
   557  }
   558  
   559  // DeployerConnectionValues containers the result of deployer.ConnectionInfo
   560  // API call.
   561  type DeployerConnectionValues struct {
   562  	StateAddresses []string
   563  	APIAddresses   []string
   564  }
   565  
   566  // StatusParams holds parameters for the Status call.
   567  type StatusParams struct {
   568  	Patterns []string
   569  }
   570  
   571  // SetRsyslogCertParams holds parameters for the SetRsyslogCert call.
   572  type SetRsyslogCertParams struct {
   573  	CACert []byte
   574  }