github.com/mattyw/juju@v0.0.0-20140610034352-732aecd63861/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 "github.com/juju/utils/proxy" 12 13 "github.com/juju/juju/charm" 14 "github.com/juju/juju/constraints" 15 "github.com/juju/juju/instance" 16 "github.com/juju/juju/utils/ssh" 17 "github.com/juju/juju/version" 18 ) 19 20 // Entity identifies a single entity. 21 type Entity struct { 22 Tag string 23 } 24 25 // Entities identifies multiple entities. 26 type Entities struct { 27 Entities []Entity 28 } 29 30 // EntityPasswords holds the parameters for making a SetPasswords call. 31 type EntityPasswords struct { 32 Changes []EntityPassword 33 } 34 35 // EntityPassword specifies a password change for the entity 36 // with the given tag. 37 type EntityPassword struct { 38 Tag string 39 Password string 40 } 41 42 // ErrorResults holds the results of calling a bulk operation which 43 // returns no data, only an error result. The order and 44 // number of elements matches the operations specified in the request. 45 type ErrorResults struct { 46 // Results contains the error results from each operation. 47 Results []ErrorResult 48 } 49 50 // OneError returns the error from the result 51 // of a bulk operation on a single value. 52 func (result ErrorResults) OneError() error { 53 if n := len(result.Results); n != 1 { 54 return fmt.Errorf("expected 1 result, got %d", n) 55 } 56 if err := result.Results[0].Error; err != nil { 57 return err 58 } 59 return nil 60 } 61 62 // ErrorResult holds the error status of a single operation. 63 type ErrorResult struct { 64 Error *Error 65 } 66 67 // StatusData contains additional information for a status. 68 type StatusData map[string]interface{} 69 70 // AddRelation holds the parameters for making the AddRelation call. 71 // The endpoints specified are unordered. 72 type AddRelation struct { 73 Endpoints []string 74 } 75 76 // AddRelationResults holds the results of a AddRelation call. The Endpoints 77 // field maps service names to the involved endpoints. 78 type AddRelationResults struct { 79 Endpoints map[string]charm.Relation 80 } 81 82 // DestroyRelation holds the parameters for making the DestroyRelation call. 83 // The endpoints specified are unordered. 84 type DestroyRelation struct { 85 Endpoints []string 86 } 87 88 // AddMachineParams encapsulates the parameters used to create a new machine. 89 type AddMachineParams struct { 90 // The following fields hold attributes that will be given to the 91 // new machine when it is created. 92 Series string 93 Constraints constraints.Value 94 Jobs []MachineJob 95 96 // If Placement is non-nil, it contains a placement directive 97 // that will be used to decide how to instantiate the machine. 98 Placement *instance.Placement 99 100 // If ParentId is non-empty, it specifies the id of the 101 // parent machine within which the new machine will 102 // be created. In that case, ContainerType must also be 103 // set. 104 ParentId string 105 106 // ContainerType optionally gives the container type of the 107 // new machine. If it is non-empty, the new machine 108 // will be implemented by a container. If it is specified 109 // but ParentId is empty, a new top level machine will 110 // be created to hold the container with given series, 111 // constraints and jobs. 112 ContainerType instance.ContainerType 113 114 // If InstanceId is non-empty, it will be associated with 115 // the new machine along with the given nonce, 116 // hardware characteristics and addresses. 117 // All the following fields will be ignored if ContainerType 118 // is set. 119 InstanceId instance.Id 120 Nonce string 121 HardwareCharacteristics instance.HardwareCharacteristics 122 Addrs []instance.Address 123 } 124 125 // AddMachines holds the parameters for making the 126 // AddMachinesWithPlacement call. 127 type AddMachines struct { 128 MachineParams []AddMachineParams 129 } 130 131 // AddMachinesResults holds the results of an AddMachines call. 132 type AddMachinesResults struct { 133 Machines []AddMachinesResult 134 } 135 136 // AddMachinesResults holds the name of a machine added by the 137 // state.api.client.AddMachine call for a single machine. 138 type AddMachinesResult struct { 139 Machine string 140 Error *Error 141 } 142 143 // DestroyMachines holds parameters for the DestroyMachines call. 144 type DestroyMachines struct { 145 MachineNames []string 146 Force bool 147 } 148 149 // ServiceDeploy holds the parameters for making the ServiceDeploy call. 150 type ServiceDeploy struct { 151 ServiceName string 152 CharmUrl string 153 NumUnits int 154 Config map[string]string 155 ConfigYAML string // Takes precedence over config if both are present. 156 Constraints constraints.Value 157 ToMachineSpec string 158 Networks []string 159 } 160 161 // ServiceUpdate holds the parameters for making the ServiceUpdate call. 162 type ServiceUpdate struct { 163 ServiceName string 164 CharmUrl string 165 ForceCharmUrl bool 166 MinUnits *int 167 SettingsStrings map[string]string 168 SettingsYAML string // Takes precedence over SettingsStrings if both are present. 169 Constraints *constraints.Value 170 } 171 172 // ServiceSetCharm sets the charm for a given service. 173 type ServiceSetCharm struct { 174 ServiceName string 175 CharmUrl string 176 Force bool 177 } 178 179 // ServiceExpose holds the parameters for making the ServiceExpose call. 180 type ServiceExpose struct { 181 ServiceName string 182 } 183 184 // ServiceSet holds the parameters for a ServiceSet 185 // command. Options contains the configuration data. 186 type ServiceSet struct { 187 ServiceName string 188 Options map[string]string 189 } 190 191 // ServiceSetYAML holds the parameters for 192 // a ServiceSetYAML command. Config contains the 193 // configuration data in YAML format. 194 type ServiceSetYAML struct { 195 ServiceName string 196 Config string 197 } 198 199 // ServiceUnset holds the parameters for a ServiceUnset 200 // command. Options contains the option attribute names 201 // to unset. 202 type ServiceUnset struct { 203 ServiceName string 204 Options []string 205 } 206 207 // ServiceGet holds parameters for making the ServiceGet or 208 // ServiceGetCharmURL calls. 209 type ServiceGet struct { 210 ServiceName string 211 } 212 213 // ServiceGetResults holds results of the ServiceGet call. 214 type ServiceGetResults struct { 215 Service string 216 Charm string 217 Config map[string]interface{} 218 Constraints constraints.Value 219 } 220 221 // ServiceCharmRelations holds parameters for making the ServiceCharmRelations call. 222 type ServiceCharmRelations struct { 223 ServiceName string 224 } 225 226 // ServiceCharmRelationsResults holds the results of the ServiceCharmRelations call. 227 type ServiceCharmRelationsResults struct { 228 CharmRelations []string 229 } 230 231 // ServiceUnexpose holds parameters for the ServiceUnexpose call. 232 type ServiceUnexpose struct { 233 ServiceName string 234 } 235 236 // PublicAddress holds parameters for the PublicAddress call. 237 type PublicAddress struct { 238 Target string 239 } 240 241 // PublicAddressResults holds results of the PublicAddress call. 242 type PublicAddressResults struct { 243 PublicAddress string 244 } 245 246 // PrivateAddress holds parameters for the PrivateAddress call. 247 type PrivateAddress struct { 248 Target string 249 } 250 251 // PrivateAddressResults holds results of the PrivateAddress call. 252 type PrivateAddressResults struct { 253 PrivateAddress string 254 } 255 256 // Resolved holds parameters for the Resolved call. 257 type Resolved struct { 258 UnitName string 259 Retry bool 260 } 261 262 // ResolvedResults holds results of the Resolved call. 263 type ResolvedResults struct { 264 Service string 265 Charm string 266 Settings map[string]interface{} 267 } 268 269 // AddServiceUnitsResults holds the names of the units added by the 270 // AddServiceUnits call. 271 type AddServiceUnitsResults struct { 272 Units []string 273 } 274 275 // AddServiceUnits holds parameters for the AddUnits call. 276 type AddServiceUnits struct { 277 ServiceName string 278 NumUnits int 279 ToMachineSpec string 280 } 281 282 // DestroyServiceUnits holds parameters for the DestroyUnits call. 283 type DestroyServiceUnits struct { 284 UnitNames []string 285 } 286 287 // ServiceDestroy holds the parameters for making the ServiceDestroy call. 288 type ServiceDestroy struct { 289 ServiceName string 290 } 291 292 // Creds holds credentials for identifying an entity. 293 type Creds struct { 294 AuthTag string 295 Password string 296 Nonce string 297 } 298 299 // GetAnnotationsResults holds annotations associated with an entity. 300 type GetAnnotationsResults struct { 301 Annotations map[string]string 302 } 303 304 // GetAnnotations stores parameters for making the GetAnnotations call. 305 type GetAnnotations struct { 306 Tag string 307 } 308 309 // SetAnnotations stores parameters for making the SetAnnotations call. 310 type SetAnnotations struct { 311 Tag string 312 Pairs map[string]string 313 } 314 315 // GetServiceConstraints stores parameters for making the GetServiceConstraints call. 316 type GetServiceConstraints struct { 317 ServiceName string 318 } 319 320 // GetConstraintsResults holds results of the GetConstraints call. 321 type GetConstraintsResults struct { 322 Constraints constraints.Value 323 } 324 325 // SetConstraints stores parameters for making the SetConstraints call. 326 type SetConstraints struct { 327 ServiceName string //optional, if empty, environment constraints are set. 328 Constraints constraints.Value 329 } 330 331 // CharmInfo stores parameters for a CharmInfo call. 332 type CharmInfo struct { 333 CharmURL string 334 } 335 336 // ResolveCharms stores charm references for a ResolveCharms call. 337 type ResolveCharms struct { 338 References []charm.Reference 339 } 340 341 // ResolveCharmResult holds the result of resolving a charm reference to a URL, or any error that occurred. 342 type ResolveCharmResult struct { 343 URL *charm.URL `json:",omitempty"` 344 Error string `json:",omitempty"` 345 } 346 347 // ResolveCharmResults holds results of the ResolveCharms call. 348 type ResolveCharmResults struct { 349 URLs []ResolveCharmResult 350 } 351 352 // AllWatcherId holds the id of an AllWatcher. 353 type AllWatcherId struct { 354 AllWatcherId string 355 } 356 357 // AllWatcherNextResults holds deltas returned from calling AllWatcher.Next(). 358 type AllWatcherNextResults struct { 359 Deltas []Delta 360 } 361 362 // Delta holds details of a change to the environment. 363 type Delta struct { 364 // If Removed is true, the entity has been removed; 365 // otherwise it has been created or changed. 366 Removed bool 367 // Entity holds data about the entity that has changed. 368 Entity EntityInfo 369 } 370 371 // ListSSHKeys stores parameters used for a KeyManager.ListKeys call. 372 type ListSSHKeys struct { 373 Entities 374 Mode ssh.ListMode 375 } 376 377 // ModifySSHKeys stores parameters used for a KeyManager.Add|Delete|Import call for a user. 378 type ModifyUserSSHKeys struct { 379 User string 380 Keys []string 381 } 382 383 // ModifyUsers holds the parameters for making a UserManager Add or Modify calls. 384 type ModifyUsers struct { 385 Changes []ModifyUser 386 } 387 388 // ModifyUser stores the parameters used for a UserManager.Add|Remove call 389 type ModifyUser struct { 390 // Tag is here purely for backwards compatability. Older clients will 391 // attempt to use the EntityPassword structure, so we need a Tag here 392 // (which will be treated as Username) 393 Tag string 394 Username string 395 DisplayName string 396 Password string 397 } 398 399 // MarshalJSON implements json.Marshaler. 400 func (d *Delta) MarshalJSON() ([]byte, error) { 401 b, err := json.Marshal(d.Entity) 402 if err != nil { 403 return nil, err 404 } 405 var buf bytes.Buffer 406 buf.WriteByte('[') 407 c := "change" 408 if d.Removed { 409 c = "remove" 410 } 411 fmt.Fprintf(&buf, "%q,%q,", d.Entity.EntityId().Kind, c) 412 buf.Write(b) 413 buf.WriteByte(']') 414 return buf.Bytes(), nil 415 } 416 417 // UnmarshalJSON implements json.Unmarshaler. 418 func (d *Delta) UnmarshalJSON(data []byte) error { 419 var elements []json.RawMessage 420 if err := json.Unmarshal(data, &elements); err != nil { 421 return err 422 } 423 if len(elements) != 3 { 424 return fmt.Errorf( 425 "Expected 3 elements in top-level of JSON but got %d", 426 len(elements)) 427 } 428 var entityKind, operation string 429 if err := json.Unmarshal(elements[0], &entityKind); err != nil { 430 return err 431 } 432 if err := json.Unmarshal(elements[1], &operation); err != nil { 433 return err 434 } 435 if operation == "remove" { 436 d.Removed = true 437 } else if operation != "change" { 438 return fmt.Errorf("Unexpected operation %q", operation) 439 } 440 switch entityKind { 441 case "machine": 442 d.Entity = new(MachineInfo) 443 case "service": 444 d.Entity = new(ServiceInfo) 445 case "unit": 446 d.Entity = new(UnitInfo) 447 case "relation": 448 d.Entity = new(RelationInfo) 449 case "annotation": 450 d.Entity = new(AnnotationInfo) 451 default: 452 return fmt.Errorf("Unexpected entity name %q", entityKind) 453 } 454 if err := json.Unmarshal(elements[2], &d.Entity); err != nil { 455 return err 456 } 457 return nil 458 } 459 460 // EntityInfo is implemented by all entity Info types. 461 type EntityInfo interface { 462 // EntityId returns an identifier that will uniquely 463 // identify the entity within its kind 464 EntityId() EntityId 465 } 466 467 // IMPORTANT NOTE: the types below are direct subsets of the entity docs 468 // held in mongo, as defined in the state package (serviceDoc, 469 // machineDoc etc). 470 // In particular, the document marshalled into mongo 471 // must unmarshal correctly into these documents. 472 // If the format of a field in a document is changed in mongo, or 473 // a field is removed and it coincides with one of the 474 // fields below, a similar change must be made here. 475 // 476 // MachineInfo corresponds with state.machineDoc. 477 // ServiceInfo corresponds with state.serviceDoc. 478 // UnitInfo corresponds with state.unitDoc. 479 // RelationInfo corresponds with state.relationDoc. 480 // AnnotationInfo corresponds with state.annotatorDoc. 481 482 var ( 483 _ EntityInfo = (*MachineInfo)(nil) 484 _ EntityInfo = (*ServiceInfo)(nil) 485 _ EntityInfo = (*UnitInfo)(nil) 486 _ EntityInfo = (*RelationInfo)(nil) 487 _ EntityInfo = (*AnnotationInfo)(nil) 488 ) 489 490 type EntityId struct { 491 Kind string 492 Id interface{} 493 } 494 495 // StateServingInfo holds information needed by a state 496 // server. 497 type StateServingInfo struct { 498 APIPort int 499 StatePort int 500 Cert string 501 PrivateKey string 502 // this will be passed as the KeyFile argument to MongoDB 503 SharedSecret string 504 SystemIdentity string 505 } 506 507 // IsMasterResult holds the result of an IsMaster API call. 508 type IsMasterResult struct { 509 // Master reports whether the connected agent 510 // lives on the same instance as the mongo replica 511 // set master. 512 Master bool 513 } 514 515 // MachineInfo holds the information about a Machine 516 // that is watched by StateWatcher. 517 type MachineInfo struct { 518 Id string `bson:"_id"` 519 InstanceId string 520 Status Status 521 StatusInfo string 522 StatusData StatusData 523 Life Life 524 Series string 525 SupportedContainers []instance.ContainerType 526 SupportedContainersKnown bool 527 HardwareCharacteristics *instance.HardwareCharacteristics `json:",omitempty"` 528 Jobs []MachineJob 529 Addresses []instance.Address 530 } 531 532 func (i *MachineInfo) EntityId() EntityId { 533 return EntityId{ 534 Kind: "machine", 535 Id: i.Id, 536 } 537 } 538 539 type ServiceInfo struct { 540 Name string `bson:"_id"` 541 Exposed bool 542 CharmURL string 543 OwnerTag string 544 Life Life 545 MinUnits int 546 Constraints constraints.Value 547 Config map[string]interface{} 548 } 549 550 func (i *ServiceInfo) EntityId() EntityId { 551 return EntityId{ 552 Kind: "service", 553 Id: i.Name, 554 } 555 } 556 557 type UnitInfo struct { 558 Name string `bson:"_id"` 559 Service string 560 Series string 561 CharmURL string 562 PublicAddress string 563 PrivateAddress string 564 MachineId string 565 Ports []instance.Port 566 Status Status 567 StatusInfo string 568 StatusData StatusData 569 } 570 571 func (i *UnitInfo) EntityId() EntityId { 572 return EntityId{ 573 Kind: "unit", 574 Id: i.Name, 575 } 576 } 577 578 type Endpoint struct { 579 ServiceName string 580 Relation charm.Relation 581 } 582 583 type RelationInfo struct { 584 Key string `bson:"_id"` 585 Id int 586 Endpoints []Endpoint 587 } 588 589 func (i *RelationInfo) EntityId() EntityId { 590 return EntityId{ 591 Kind: "relation", 592 Id: i.Key, 593 } 594 } 595 596 type AnnotationInfo struct { 597 Tag string 598 Annotations map[string]string 599 } 600 601 func (i *AnnotationInfo) EntityId() EntityId { 602 return EntityId{ 603 Kind: "annotation", 604 Id: i.Tag, 605 } 606 } 607 608 // ContainerManagerConfigParams contains the parameters for the 609 // ContainerManagerConfig provisioner API call. 610 type ContainerManagerConfigParams struct { 611 Type instance.ContainerType 612 } 613 614 // ContainerManagerConfig contains information from the environment config 615 // that is needed for configuring the container manager. 616 type ContainerManagerConfig struct { 617 ManagerConfig map[string]string 618 } 619 620 // ContainerConfig contains information from the environment config that is 621 // needed for container cloud-init. 622 type ContainerConfig struct { 623 ProviderType string 624 AuthorizedKeys string 625 SSLHostnameVerification bool 626 Proxy proxy.Settings 627 AptProxy proxy.Settings 628 } 629 630 // ProvisioningScriptParams contains the parameters for the 631 // ProvisioningScript client API call. 632 type ProvisioningScriptParams struct { 633 MachineId string 634 Nonce string 635 636 // DataDir may be "", in which case the default will be used. 637 DataDir string 638 639 // DisablePackageCommands may be set to disable all package-related 640 // commands. It is then the responsibility of the provisioner to 641 // ensure that all the packages required by Juju are available. 642 DisablePackageCommands bool 643 } 644 645 // ProvisioningScriptResult contains the result of the 646 // ProvisioningScript client API call. 647 type ProvisioningScriptResult struct { 648 Script string 649 } 650 651 // EnvironmentGetResults contains the result of EnvironmentGet client 652 // API call. 653 type EnvironmentGetResults struct { 654 Config map[string]interface{} 655 } 656 657 // EnvironmentSet contains the arguments for EnvironmentSet client API 658 // call. 659 type EnvironmentSet struct { 660 Config map[string]interface{} 661 } 662 663 // EnvironmentUnset contains the arguments for EnvironmentUnset client API 664 // call. 665 type EnvironmentUnset struct { 666 Keys []string 667 } 668 669 // SetEnvironAgentVersion contains the arguments for 670 // SetEnvironAgentVersion client API call. 671 type SetEnvironAgentVersion struct { 672 Version version.Number 673 } 674 675 // DeployerConnectionValues containers the result of deployer.ConnectionInfo 676 // API call. 677 type DeployerConnectionValues struct { 678 StateAddresses []string 679 APIAddresses []string 680 } 681 682 // StatusParams holds parameters for the Status call. 683 type StatusParams struct { 684 Patterns []string 685 } 686 687 // SetRsyslogCertParams holds parameters for the SetRsyslogCert call. 688 type SetRsyslogCertParams struct { 689 CACert []byte 690 } 691 692 // RsyslogConfigResult holds the result of a GetRsyslogConfig call. 693 type RsyslogConfigResult struct { 694 Error *Error 695 CACert string 696 // Port is only used by state servers as the port to listen on. 697 // Clients should use HostPorts for the rsyslog addresses to forward 698 // logs to. 699 Port int 700 HostPorts []instance.HostPort 701 } 702 703 // RsyslogConfigResults is the bulk form of RyslogConfigResult 704 type RsyslogConfigResults struct { 705 Results []RsyslogConfigResult 706 } 707 708 // DistributionGroupResult contains the result of 709 // the DistributionGroup provisioner API call. 710 type DistributionGroupResult struct { 711 Error *Error 712 Result []instance.Id 713 } 714 715 // DistributionGroupResults is the bulk form of 716 // DistributionGroupResult. 717 type DistributionGroupResults struct { 718 Results []DistributionGroupResult 719 } 720 721 // APIHostPortsResult holds the result of an APIHostPorts 722 // call. Each element in the top level slice holds 723 // the addresses for one API server. 724 type APIHostPortsResult struct { 725 Servers [][]instance.HostPort 726 } 727 728 // LoginResult holds the result of a Login call. 729 type LoginResult struct { 730 Servers [][]instance.HostPort 731 EnvironTag string 732 } 733 734 // EnsureAvailability contains arguments for 735 // the EnsureAvailability client API call. 736 type EnsureAvailability struct { 737 NumStateServers int 738 Constraints constraints.Value 739 // Series is the series to associate with new state server machines. 740 // If this is empty, then the environment's default series is used. 741 Series string 742 }