コード例 #1
0
        /// <summary>
        /// Creates a session with the server using username and password
        /// </summary>
        /// <param name="server">hostname of the server to login</param>
        /// <param name="username">username for login</param>
        /// <param name="password">password for login</param>
        /// <returns>the stub configuration configured with an authenticated
        ///          session
        /// </returns>
        public StubConfiguration LoginByUsernameAndPassword(string server,
                                                            string username, string password)
        {
            if (this.sessionSvc != null)
            {
                throw new Exception("Session already created");
            }

            StubFactory = CreateApiStubFactory(server);

            // Create a security context for username/password authentication
            ExecutionContext.SecurityContext securityContext =
                new UserPassSecurityContext(
                    username, password.ToCharArray());

            /*
             * Create a stub configuration with username/password security
             * context
             */
            StubConfiguration stubConfig = new StubConfiguration();

            stubConfig.SetSecurityContext(securityContext);

            // Create a session stub using the stub configuration.
            Session session =
                StubFactory.CreateStub <Session>(stubConfig);

            // Login and create a session
            char[] sessionId = session.Create();

            /*
             * Initialize a session security context from the generated
             * session id
             */
            SessionSecurityContext sessionSecurityContext =
                new SessionSecurityContext(sessionId);

            // Update the stub configuration to use the session id
            stubConfig.SetSecurityContext(sessionSecurityContext);

            /*
             * Create a stub for the session service using the authenticated
             * session
             */
            this.sessionSvc =
                StubFactory.CreateStub <Session>(stubConfig);
            return(stubConfig);
        }
コード例 #2
0
        /// <summary>
        /// Returns the identifier of the datacenter.
        ///
        /// Note: The method assumes that there is only one datacenter with the
        /// specified name
        /// </summary>
        /// <param name="stubFactory">Stub factory for api endpoint</param>
        /// <param name="sessionStubConfig">stub configuration for the current
        ///  session
        /// </param>
        /// <param name="datacenterName">name of the datacenter</param>
        /// <returns>identifier of a datacenter</returns>
        public static String GetDatacenter(
            StubFactory stubFactory, StubConfiguration sessionStubConfig,
            string datacenterName)
        {
            Datacenter datacenterService =
                stubFactory.CreateStub <Datacenter>(sessionStubConfig);
            HashSet <String> datacenterNames = new HashSet <String>
            {
                datacenterName
            };

            DatacenterTypes.FilterSpec dcFilterSpec =
                new DatacenterTypes.FilterSpec();
            dcFilterSpec.SetNames(datacenterNames);
            List <DatacenterTypes.Summary> dcSummaries =
                datacenterService.List(dcFilterSpec);

            if (dcSummaries.Count > 1)
            {
                throw new Exception(String.Format("More than one datacenter" +
                                                  " with the specified name {0} exist", datacenterName));
            }

            if (dcSummaries.Count <= 0)
            {
                throw new Exception(String.Format("Datacenter with name {0}" +
                                                  " not found !", datacenterName));
            }

            return(dcSummaries[0].GetDatacenter());
        }
コード例 #3
0
        /// <summary>
        /// Returns the identifier of a VM
        ///
        /// Note: The method assumes that there is only one VM and datacenter
        ///  with the specified names.
        /// </summary>
        /// <param name="stubFactory">Stub factory for api endpoint</param>
        /// <param name="sessionStubConfig">stub configuration for the current
        ///  session
        /// </param>
        /// <param name="vmName">name of the vm</param>
        /// <returns>the identifier of a VM</returns>
        public static String GetVm(StubFactory stubFactory,
                                   StubConfiguration sessionStubConfig, string vmName)
        {
            VMTypes.FilterSpec vmFilterSpec = new VMTypes.FilterSpec();
            vmFilterSpec.SetNames(new HashSet <String> {
                vmName
            });

            VM vmService = stubFactory.CreateStub <VM>(sessionStubConfig);
            List <VMTypes.Summary> vmSummaries = vmService.List(vmFilterSpec);

            if (vmSummaries.Count > 1)
            {
                throw new Exception(String.Format("More than one vm" +
                                                  " with the specified name {0} exist",
                                                  vmName));
            }

            if (vmSummaries.Count <= 0)
            {
                throw new Exception(String.Format("VM with name {0}" +
                                                  "not found !", vmName));
            }

            return(vmSummaries[0].GetVm());
        }
コード例 #4
0
        /// <summary>
        /// Creates a session with the server using SAML Bearer Token
        /// </summary>
        /// <param name="server">hostname of the server to login</param>
        /// <param name="username">username for login</param>
        /// <param name="password">password for login</param>
        /// <returns>the stub configuration configured with an authenticated
        ///          session
        /// </returns>
        public StubConfiguration LoginBySamlBearerToken(string server,
                                                        SamlToken samlBearerToken)
        {
            if (this.sessionSvc != null)
            {
                throw new Exception("Session already created");
            }

            StubFactory = CreateApiStubFactory(server);

            // Create a SAML security context using SAML bearer token
            ExecutionContext.SecurityContext samlSecurityContext = new SamlTokenSecurityContext(
                samlBearerToken, null);

            /*
             * Create a stub configuration with username/password security
             * context
             */
            StubConfiguration stubConfig = new StubConfiguration();

            stubConfig.SetSecurityContext(samlSecurityContext);

            // Create a session stub using the stub configuration.
            Session session =
                StubFactory.CreateStub <Session>(stubConfig);

            // Login and create a session
            char[] sessionId = session.Create();

            /*
             * Initialize a session security context from the generated
             * session id
             */
            SessionSecurityContext sessionSecurityContext =
                new SessionSecurityContext(sessionId);

            // Update the stub configuration to use the session id
            stubConfig.SetSecurityContext(sessionSecurityContext);

            /*
             * Create a stub for the session service using the authenticated
             * session
             */
            this.sessionSvc =
                StubFactory.CreateStub <Session>(stubConfig);
            return(stubConfig);
        }
コード例 #5
0
        /// <summary>
        /// Returns the identifier of a distributed network
        ///
        /// Note: The method assumes that there is only one distributed portgroup
        ///  and datacenter with the specified names.
        /// </summary>
        /// <param name="serviceManager">Helper for instantiating vapi services</param>
        /// <param name="datacenterName">name of the datacenter</param>
        /// <param name="distPortgroupName">name of the distributed portgroup</param>
        /// <returns>identifier of the distributed network</returns>
        public static string GetDistributedNetworkBacking(
            StubFactory stubFactory, StubConfiguration sessionStubConfig,
            string datacenterName, string distPortgroupName)
        {
            HashSet<string> datacenters = new HashSet<string>
            {
                DatacenterHelper.GetDatacenter(
                    stubFactory, sessionStubConfig, datacenterName)
            };
            NetworkTypes.FilterSpec networkFilterSpec =
                new NetworkTypes.FilterSpec();
            networkFilterSpec.SetNames(
                new HashSet<string> { distPortgroupName });
            networkFilterSpec.SetDatacenters(datacenters);
            networkFilterSpec.SetTypes(new HashSet<NetworkTypes.Type>
            {
                NetworkTypes.Type.DISTRIBUTED_PORTGROUP
            });
            Network networkService =
                stubFactory.CreateStub<Network>(sessionStubConfig);
            List<NetworkTypes.Summary> networkSummaries =
                networkService.List(networkFilterSpec);

            if (networkSummaries.Count > 1)
            {
                throw new Exception(String.Format("More than one distributed" +
                    " portgroup with the specified name {0} exist",
                    distPortgroupName));

            }

            if (networkSummaries.Count <= 0)
            {
                throw new Exception(String.Format("Distributed portgroup " +
                                    "with name {0} not found !",
                                    distPortgroupName));
            }

            return networkSummaries[0].GetNetwork();
        }
コード例 #6
0
        /// <summary>
        /// Returns the identifier of a cluster.
        ///
        /// Note: The method assumes that there is only one cluster and
        /// datacenter with the specified names
        /// </summary>
        /// <param name="stubFactory">Stub factory for api endpoint</param>
        /// <param name="sessionStubConfig">stub configuration for the current
        ///  session
        /// </param>
        /// <param name="datacenterName">name of the datacenter</param>
        /// <param name="clusterName">name of the cluster</param>
        /// <returns>identifier of the cluster</returns>
        public static String GetCluster(
            StubFactory stubFactory, StubConfiguration sessionStubConfig,
            string datacenterName, string clusterName)
        {
            HashSet <string> datacenters = new HashSet <string>
            {
                DatacenterHelper.GetDatacenter(stubFactory, sessionStubConfig,
                                               datacenterName)
            };

            ClusterTypes.FilterSpec clusterFilterSpec =
                new ClusterTypes.FilterSpec();
            HashSet <string> clusters = new HashSet <string> {
                clusterName
            };

            clusterFilterSpec.SetNames(clusters);
            clusterFilterSpec.SetDatacenters(datacenters);

            Cluster clusterService = stubFactory.CreateStub <Cluster>(
                sessionStubConfig);
            List <ClusterTypes.Summary> clusterSummaries =
                clusterService.List(clusterFilterSpec);

            if (clusterSummaries.Count > 1)
            {
                throw new Exception(String.Format("More than one cluster with"
                                                  + " the specified name {0} exist", clusterName));
            }

            if (clusterSummaries.Count <= 0)
            {
                throw new Exception(String.Format("Cluster with name {0}" +
                                                  " not found !", clusterName));
            }

            return(clusterSummaries[0].GetCluster());
        }
コード例 #7
0
        /// <summary>
        /// Returns the identifier of a folder.
        ///
        /// Note: The method assumes that there is only one folder and
        ///  datacenter with the specified names.
        /// </summary>
        /// <param name="stubFactory">Stub factory for api endpoint</param>
        /// <param name="sessionStubConfig">stub configuration for the current
        ///  session
        /// </param>
        /// <param name="datacenterName">name of the datacenter</param>
        /// <param name="folderName">name of the folder</param>
        /// <returns>identifier of a folder</returns>
        public static String GetFolder(
            StubFactory stubFactory, StubConfiguration sessionStubConfig,
            string datacenterName, string folderName)
        {
            HashSet <string> datacenters = new HashSet <string>
            {
                DatacenterHelper.GetDatacenter(
                    stubFactory, sessionStubConfig, datacenterName)
            };

            FolderTypes.FilterSpec folderFilterSpec =
                new FolderTypes.FilterSpec();
            folderFilterSpec.SetNames(new HashSet <String> {
                folderName
            });
            folderFilterSpec.SetDatacenters(datacenters);

            Folder folderService = stubFactory.CreateStub <Folder>(
                sessionStubConfig);
            List <FolderTypes.Summary> folderSummaries =
                folderService.List(folderFilterSpec);

            if (folderSummaries.Count > 1)
            {
                throw new Exception(String.Format("More than one folder" +
                                                  " with the specified name {0} exist", folderName));
            }

            if (folderSummaries.Count <= 0)
            {
                throw new Exception(String.Format("Folder with name {0}" +
                                                  "not found !", folderName));
            }

            return(folderSummaries[0].GetFolder());
        }
コード例 #8
0
        /// <summary>
        /// Returns the identifier of a datastore.
        ///
        /// Note: The method assumes that there is only one datacenter and
        /// datastore with the specified names.
        /// </summary>
        /// <param name="stubFactory">Stub factory for api endpoint</param>
        /// <param name="sessionStubConfig">stub configuration for the current
        ///  session
        /// </param>
        /// <param name="datacenterName">name of the datacenter</param>
        /// <param name="datastoreName">name of the datastore</param>
        /// <returns>identifier of a datastore</returns>
        public static String GetDatastore(
            StubFactory stubFactory, StubConfiguration sessionStubConfig,
            string datacenterName, string datastoreName)
        {
            HashSet <string> datacenters = new HashSet <string>
            {
                DatacenterHelper.GetDatacenter(
                    stubFactory, sessionStubConfig, datacenterName)
            };

            DatastoreTypes.FilterSpec dsFilterSpec =
                new DatastoreTypes.FilterSpec();
            dsFilterSpec.SetNames(new HashSet <string> {
                datastoreName
            });
            dsFilterSpec.SetDatacenters(datacenters);

            Datastore datastoreService =
                stubFactory.CreateStub <Datastore>(sessionStubConfig);
            List <DatastoreTypes.Summary> dsSummaries =
                datastoreService.List(dsFilterSpec);

            if (dsSummaries.Count > 1)
            {
                throw new Exception(String.Format("More than one datastore" +
                                                  " with the specified name {0} exist", datastoreName));
            }

            if (dsSummaries.Count <= 0)
            {
                throw new Exception(String.Format("Datastore with name {0}" +
                                                  "not found !", datastoreName));
            }

            return(dsSummaries[0].GetDatastore());
        }