client.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. Copyright (c) 2014-2016 VMware, Inc. All Rights Reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. /*
  14. This package is the root package of the govmomi library.
  15. The library is structured as follows:
  16. Package vim25
  17. The minimal usable functionality is available through the vim25 package.
  18. It contains subpackages that contain generated types, managed objects, and all
  19. available methods. The vim25 package is entirely independent of the other
  20. packages in the govmomi tree -- it has no dependencies on its peers.
  21. The vim25 package itself contains a client structure that is
  22. passed around throughout the entire library. It abstracts a session and its
  23. immutable state. See the vim25 package for more information.
  24. Package session
  25. The session package contains an abstraction for the session manager that allows
  26. a user to login and logout. It also provides access to the current session
  27. (i.e. to determine if the user is in fact logged in)
  28. Package object
  29. The object package contains wrappers for a selection of managed objects. The
  30. constructors of these objects all take a *vim25.Client, which they pass along
  31. to derived objects, if applicable.
  32. Package govc
  33. The govc package contains the govc CLI. The code in this tree is not intended
  34. to be used as a library. Any functionality that govc contains that _could_ be
  35. used as a library function but isn't, _should_ live in a root level package.
  36. Other packages
  37. Other packages, such as "event", "guest", or "license", provide wrappers for
  38. the respective subsystems. They are typically not needed in normal workflows so
  39. are kept outside the object package.
  40. */
  41. package govmomi
  42. import (
  43. "context"
  44. "net/url"
  45. "github.com/vmware/govmomi/property"
  46. "github.com/vmware/govmomi/session"
  47. "github.com/vmware/govmomi/vim25"
  48. "github.com/vmware/govmomi/vim25/soap"
  49. "github.com/vmware/govmomi/vim25/types"
  50. )
  51. type Client struct {
  52. *vim25.Client
  53. SessionManager *session.Manager
  54. }
  55. // NewClient creates a new client from a URL. The client authenticates with the
  56. // server with username/password before returning if the URL contains user information.
  57. func NewClient(ctx context.Context, u *url.URL, insecure bool) (*Client, error) {
  58. soapClient := soap.NewClient(u, insecure)
  59. vimClient, err := vim25.NewClient(ctx, soapClient)
  60. if err != nil {
  61. return nil, err
  62. }
  63. c := &Client{
  64. Client: vimClient,
  65. SessionManager: session.NewManager(vimClient),
  66. }
  67. // Only login if the URL contains user information.
  68. if u.User != nil {
  69. err = c.Login(ctx, u.User)
  70. if err != nil {
  71. return nil, err
  72. }
  73. }
  74. return c, nil
  75. }
  76. // Login dispatches to the SessionManager.
  77. func (c *Client) Login(ctx context.Context, u *url.Userinfo) error {
  78. return c.SessionManager.Login(ctx, u)
  79. }
  80. // Logout dispatches to the SessionManager.
  81. func (c *Client) Logout(ctx context.Context) error {
  82. // Close any idle connections after logging out.
  83. defer c.Client.CloseIdleConnections()
  84. return c.SessionManager.Logout(ctx)
  85. }
  86. // PropertyCollector returns the session's default property collector.
  87. func (c *Client) PropertyCollector() *property.Collector {
  88. return property.DefaultCollector(c.Client)
  89. }
  90. // RetrieveOne dispatches to the Retrieve function on the default property collector.
  91. func (c *Client) RetrieveOne(ctx context.Context, obj types.ManagedObjectReference, p []string, dst interface{}) error {
  92. return c.PropertyCollector().RetrieveOne(ctx, obj, p, dst)
  93. }
  94. // Retrieve dispatches to the Retrieve function on the default property collector.
  95. func (c *Client) Retrieve(ctx context.Context, objs []types.ManagedObjectReference, p []string, dst interface{}) error {
  96. return c.PropertyCollector().Retrieve(ctx, objs, p, dst)
  97. }
  98. // Wait dispatches to property.Wait.
  99. func (c *Client) Wait(ctx context.Context, obj types.ManagedObjectReference, ps []string, f func([]types.PropertyChange) bool) error {
  100. return property.Wait(ctx, c.PropertyCollector(), obj, ps, f)
  101. }
  102. // IsVC returns true if we are connected to a vCenter
  103. func (c *Client) IsVC() bool {
  104. return c.Client.IsVC()
  105. }