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