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