예제 #1
0
        /// <summary>
        /// Process Records
        /// </summary>
        protected override void ProcessRecord()
        {
            userProfileProvider.DeleteUserProfile();
            Session.ApiClient.Disconnect();

            WriteObject("Disconnected from the Cohesity Cluster");
        }
예제 #2
0
        /// <summary>
        /// Process Records
        /// </summary>
        protected override void ProcessRecord()
        {
            if (this.APIKey != null)
            {
                // allow the user profile for validating the api key
                var userProfile = new UserProfile
                {
                    ClusterUri  = clusterUri,
                    AccessToken = null,
                    AllowInvalidServerCertificates = true,
                    APIKey = this.APIKey
                };
                userProfileProvider.SetUserProfile(userProfile);
                if (APIKeyAdapter.ValidateAPIKey(this.Server, this.APIKey))
                {
                    WriteObject($"Connected to the Cohesity Cluster {Server} Successfully");
                    return;
                }
                userProfileProvider.DeleteUserProfile();
                WriteObject("Failed to connect to the Cohesity Cluster.");
                return;
            }

            var networkCredential = Credential.GetNetworkCredential();
            var domain            = string.IsNullOrWhiteSpace(networkCredential.Domain) ? LocalDomain : networkCredential.Domain;

            var credentials = new AccessTokenCredential
            {
                Domain   = domain,
                Username = networkCredential.UserName,
                Password = networkCredential.Password
            };

            var httpRequest = new HttpRequestMessage(HttpMethod.Post, "public/accessTokens")
            {
                Content = new StringContent(JsonConvert.SerializeObject(credentials), Encoding.UTF8, "application/json")
            };

            try
            {
                var httpClient      = Session.ApiClient.BuildClient(clusterUri, true);
                var response        = httpClient.SendAsync(httpRequest).Result;
                var responseContent = response.Content.ReadAsStringAsync().Result;

                if (response.StatusCode != HttpStatusCode.Created)
                {
                    var           error = JsonConvert.DeserializeObject <ErrorProto>(responseContent);
                    StringBuilder sb    = new StringBuilder();
                    sb.AppendLine("Failed to connect to the Cohesity Cluster");
                    sb.AppendLine(error.ErrorMsg);
                    throw new Exception(sb.ToString());
                }

                var accessToken = JsonConvert.DeserializeObject <AccessToken>(responseContent);

                var userProfile = new UserProfile
                {
                    ClusterUri  = clusterUri,
                    AccessToken = accessToken,
                    AllowInvalidServerCertificates = true,
                };

                userProfileProvider.SetUserProfile(userProfile);
                //saving the credentials in process environment variable
                userProfileProvider.SaveCredentials(credentials);

                WriteObject($"Connected to the Cohesity Cluster {Server} Successfully");
            }
            catch (AggregateException ex)
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("Failed to connect to the Cohesity Cluster");

                foreach (Exception exInnerException in ex.Flatten().InnerExceptions)
                {
                    Exception exNestedInnerException = exInnerException;
                    do
                    {
                        if (!string.IsNullOrEmpty(exNestedInnerException.Message))
                        {
                            sb.AppendLine(exNestedInnerException.Message);
                        }

                        exNestedInnerException = exNestedInnerException.InnerException;
                    } while (exNestedInnerException != null);
                }

                throw new Exception(sb.ToString());
            }
            catch (Exception ex)
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("Failed to connect to the Cohesity Cluster");
                sb.AppendLine(ex.Message);
                throw new Exception(sb.ToString());
            }
        }