示例#1
0
        public static SimplifiedNic GetNicDetails(String accessToken, ComputeVm Vm)
        {
            string authToken = "Bearer" + " " + accessToken;
            var    client    = new WebClient();

            client.Headers.Add("Authorization", authToken);
            client.Headers.Add("Content-Type", "application/json");

            var    nic     = Vm.properties.networkProfile.networkInterfaces.FirstOrDefault();
            string nicLink = nic.id;
            String nicUrl  = String.Format("https://management.azure.com{0}{1}", nicLink, "?api-version=2015-05-01-preview");
            String nicText = client.DownloadString(nicUrl);

            //ComputeVms rgCompVms = JsonConvert.DeserializeObject<ComputeVms>(rgVmsText);
            ArmRest.Models.NetworkInterfaceDetails.RootObject nicObj = JsonConvert.DeserializeObject <ArmRest.Models.NetworkInterfaceDetails.RootObject>(nicText);

            String InternalIpAddress = nicObj.properties.ipConfigurations.FirstOrDefault().properties.privateIPAddress;

            ArmRest.Models.PublicIpAddress.RootObject publicIpAddressObj = new Models.PublicIpAddress.RootObject();
            if (nicObj.properties.ipConfigurations.FirstOrDefault().properties.publicIPAddress != null)
            {
                String PublicIpLink = nicObj.properties.ipConfigurations.FirstOrDefault().properties.publicIPAddress.id;
                String publicIpUrl  = String.Format("https://management.azure.com{0}{1}", PublicIpLink, "?api-version=2015-05-01-preview");
                String publicIpText = client.DownloadString(publicIpUrl);
                publicIpAddressObj = JsonConvert.DeserializeObject <ArmRest.Models.PublicIpAddress.RootObject>(publicIpText);
            }

            SimplifiedNic thisSimplifiedNic = new SimplifiedNic();

            thisSimplifiedNic.InternalIpAddress = InternalIpAddress;


            String PublicIpAddress = null;

            if (publicIpAddressObj != null)
            {
                try
                {
                    PublicIpAddress = publicIpAddressObj.properties.ipAddress;
                    thisSimplifiedNic.PublicIpAddress = PublicIpAddress;
                }
                catch
                { }

                try
                {
                    thisSimplifiedNic.PublicHostName = publicIpAddressObj.properties.dnsSettings.fqdn;
                }
                catch
                { }
            }



            return(thisSimplifiedNic);
        }
示例#2
0
        public static bool GetVmPowerStatus(String accessToken, ComputeVm thisVm)
        {
            bool returnResult = false;

            string vmId             = thisVm.id;
            var    SplitUrl         = vmId.Split('/');
            var    rgName           = SplitUrl[4];
            var    subscriptionName = SplitUrl[2];

            string authToken = "Bearer" + " " + accessToken;
            var    client    = new WebClient();

            client.Headers.Add("Authorization", authToken);
            client.Headers.Add("Content-Type", "application/json");

            string vmStatusUrl = string.Format("https://management.azure.com/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Compute/virtualMachines/{2}?$expand=instanceView&api-version=2015-06-15", subscriptionName, rgName, thisVm.name);
            String text        = "";

            text = client.DownloadString(vmStatusUrl);
            var vmStatus     = Newtonsoft.Json.JsonConvert.DeserializeObject <ComputeVm>(text);
            var InstanceView = vmStatus.properties.instanceView.statuses;
            var ThisInstanceviewPowerState        = InstanceView.Where(p => p.code.Contains("PowerState")).FirstOrDefault();
            var ThisInstanceviewProvisioningState = InstanceView.Where(p => p.code.Contains("ProvisioningState")).FirstOrDefault();

            try
            {
                if ((ThisInstanceviewPowerState.code == "PowerState/running") && (ThisInstanceviewProvisioningState.code == "ProvisioningState/succeeded"))
                {
                    returnResult = true;
                }
                System.Diagnostics.Debug.WriteLine(string.Format("VM {0} in RG {1} has power state {2}", thisVm.name, rgName, ThisInstanceviewPowerState.code));
            }
            catch (Exception e)
            {
                returnResult = false;
            }


            return(returnResult);
        }
示例#3
0
        public static ResourceGroup GetHostResourceGroup(String accessToken, ComputeVm thisVm)
        {
            string vmId             = thisVm.id;
            var    SplitUrl         = vmId.Split('/');
            var    rgName           = SplitUrl[4];
            var    subscriptionName = SplitUrl[2];

            string authToken = "Bearer" + " " + accessToken;
            var    client    = new WebClient();

            client.Headers.Add("Authorization", authToken);
            client.Headers.Add("Content-Type", "application/json");

            string singleRgUrl = String.Format("https://management.azure.com/subscriptions/{0}/resourcegroups/{1}?api-version=2015-01-01 ", subscriptionName, rgName);

            String text = "";

            text = client.DownloadString(singleRgUrl);
            ResourceGroup thisRg = Newtonsoft.Json.JsonConvert.DeserializeObject <ResourceGroup>(text);

            return(thisRg);
        }
示例#4
0
        public HttpResponseMessage Get(String hostName)
        {
            List <String> subList = new List <string>();

            Subscriptions subscriptions = ListSubscriptions.GetSubscriptions();

            foreach (var sub in subscriptions.value)
            {
                if (sub.id.Contains(SubscriptionFilter))
                {
                    subList.Add(sub.subscriptionId);
                }
            }

            String accessToken = "";

            accessToken = Adal.AccessToken();
            string authToken = "Bearer" + " " + accessToken;
            var    client    = new WebClient();

            client.Headers.Add("Authorization", authToken);
            client.Headers.Add("Content-Type", "application/json");
            ComputeVms masterComputeVmsList = new ComputeVms();

            masterComputeVmsList.value = new List <ComputeVm>();

            foreach (var subId in subList)
            {
                Uri    resourceGroupsUri = new Uri(String.Format("https://management.azure.com/subscriptions/{0}/providers/Microsoft.Compute/virtualmachines?api-version=2015-05-01-preview", subId));
                String text = "";
                text = client.DownloadString(resourceGroupsUri);
                ComputeVms computeVmsList = JsonConvert.DeserializeObject <ComputeVms>(text);
                foreach (var vm in computeVmsList.value)
                {
                    masterComputeVmsList.value.Add(vm);
                }
            }

            foreach (var vm in masterComputeVmsList.value)
            {
                ResourceGroup thisRg = ComputeResources.GetHostResourceGroup(accessToken, vm);
                if ((vm.tags != null) && (vm.tags.ContainsKey("AnsibleDomainSuffix")))
                {
                    vm.name = vm.name + "." + vm.tags["AnsibleDomainSuffix"];
                }
                else if ((thisRg.tags != null) && (thisRg.tags.ContainsKey("AnsibleDomainSuffix")))
                {
                    vm.name = vm.name + "." + thisRg.tags["AnsibleDomainSuffix"];
                }
            }

            ComputeVm thisComputeVm = masterComputeVmsList.value.Where(t => t.name.ToLower() == hostName.ToLower()).FirstOrDefault();

            if (thisComputeVm == null)
            {
                var response = this.Request.CreateResponse(HttpStatusCode.NotFound);
                return(response);
            }
            else
            {
                Dictionary <String, String> TagsDict = new Dictionary <String, String>();

                if (thisComputeVm.tags != null)
                {
                    foreach (var tag in thisComputeVm.tags.Where(t => t.Key.ToLower().StartsWith("ansible__")))
                    {
                        TagsDict.Add((tag.Key.ToLower().Replace("ansible__", "")), tag.Value);
                    }
                }


                String json     = JsonConvert.SerializeObject(TagsDict);
                var    response = this.Request.CreateResponse(HttpStatusCode.OK);
                response.Content = new StringContent(json, Encoding.UTF8, "application/json");
                return(response);
            }
        }