Exemple #1
0
        public async Task <ApiResponse <ClustersCatalog> > GetClusters(
            IEnumerable <string> subscriptionIds,
            string azureAccessToken)
        {
            try
            {
                using (var client = new HttpClient())
                {
                    client.BaseAddress = new Uri("https://management.azure.com");
                    client.DefaultRequestHeaders.Authorization =
                        new AuthenticationHeaderValue("Bearer", azureAccessToken);

                    var clustersCatalog = new ClustersCatalog();

                    foreach (var subscriptionId in subscriptionIds)
                    {
                        var aksClustersResponse = await client.GetAsync(
                            $"/subscriptions/{subscriptionId}/providers/Microsoft.ContainerService" +
                            $"/managedClusters?api-version={AksApiVersion}");

                        if (!aksClustersResponse.StatusCode.Equals(HttpStatusCode.OK))
                        {
                            continue;
                        }

                        clustersCatalog.Add(subscriptionId, JsonConvert.DeserializeObject <Clusters>(
                                                aksClustersResponse.Content.ReadAsStringAsync().Result));
                    }

                    return(clustersCatalog.Count.Equals(0)
                        ? new ApiResponse <ClustersCatalog>(HttpStatusCode.NotFound, null)
                        : new ApiResponse <ClustersCatalog>(HttpStatusCode.OK, clustersCatalog));
                }
            }
            catch (Exception ex)
            {
                return(new ApiResponse <ClustersCatalog>(HttpStatusCode.InternalServerError, null, ex.Message));
            }
        }
Exemple #2
0
        public async Task <ApiResponse <ClustersCatalog> > GetClustersWithAccessToken(
            IEnumerable <string> subscriptionIds,
            string azureAccessToken)
        {
            try
            {
                using (var client = new HttpClient())
                {
                    client.BaseAddress = new Uri("https://management.azure.com");
                    client.DefaultRequestHeaders.Authorization =
                        new AuthenticationHeaderValue("Bearer", azureAccessToken);

                    var clustersCatalog = new ClustersCatalog();

                    foreach (var subscriptionId in subscriptionIds)
                    {
                        var aksClustersResponse = await client.GetAsync(
                            $"/subscriptions/{subscriptionId}/providers/Microsoft.ContainerService" +
                            $"/managedClusters?api-version={AksApiVersion}");

                        if (!aksClustersResponse.StatusCode.Equals(HttpStatusCode.OK))
                        {
                            continue;
                        }

                        var clustersResponse = JsonConvert.DeserializeObject <Clusters>(
                            aksClustersResponse.Content.ReadAsStringAsync().Result);

                        var clusters = new Clusters();

                        foreach (var clusterResponse in clustersResponse.value)
                        {
                            var aksCredentialsResponse = await client.PostAsync(
                                $"{clusterResponse.id}/accessProfiles/clusterAdmin/listCredential?api-version={AksApiVersion}",
                                null);

                            if (!aksCredentialsResponse.StatusCode.Equals(HttpStatusCode.OK))
                            {
                                continue;
                            }

                            var credential = JsonConvert.DeserializeObject <ClusterDto.AdminUser>(
                                aksCredentialsResponse.Content.ReadAsStringAsync().Result);

                            var cluster     = clusterResponse;
                            var accessToken = await GetAccessTokenFromKubeconfig(credential.properties.kubeConfig);

                            cluster.accessToken = accessToken;
                            clusters.value.Add(cluster);
                        }

                        clustersCatalog.Add(subscriptionId, clusters);
                    }

                    return(clustersCatalog.Count.Equals(0)
                        ? new ApiResponse <ClustersCatalog>(HttpStatusCode.NotFound, null)
                        : new ApiResponse <ClustersCatalog>(HttpStatusCode.OK, clustersCatalog));
                }
            }
            catch (Exception ex)
            {
                return(new ApiResponse <ClustersCatalog>(HttpStatusCode.InternalServerError, null, ex.Message));
            }
        }