Example #1
0
        static void RetrieveRMResources()
        {
            Console.WriteLine("Start to sync RM resources ...");
            string _subscriptionId = ConfigurationSettings.AppSettings["azureSubscriptionID"];
            var client = new HttpClient();
            var header = AuthenticationHelper.GetAuthorizationHeader();
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", header);

            try
            {
                var myTask = client.GetStringAsync(
                    String.Format(
                        "https://management.chinacloudapi.cn/subscriptions/{0}/resources?api-version=2015-01-01",
                        _subscriptionId));
                var result = myTask.Result;

                JObject jObj = JObject.Parse(result);
                JToken jTk = jObj.GetValue("value").First;

                while (jTk != null)
                {
                    string resourceId = jTk["id"].ToString(); //  /subscriptions/03042fd8-7b09-4c73-9217-0dcea66ede69/resourceGroups/Ambercs/providers/Microsoft.ClassicCompute/domainNames/Ambercs
                    string[] items = resourceId.Split('/');
                    string resourceGroupName = "";
                    string resourceType = "";
                    for(int i=0; i<items.Count() - 1;i++)
                    {
                        if(items[i]=="resourceGroups")
                        {
                            resourceGroupName = items[i+1];
                        }
                        if(items[i]=="providers")
                        {
                            resourceType = items[i + 1] + "." + items[i + 2] + "." + items[i + 3];
                            break;
                        }
                    }

                    var rmResource = new RMResourceEntity(resourceGroupName, resourceType);
                    rmResource.resourceID = resourceId;
                    rmResource.resourceName = jTk["name"].ToString();
                    rmResource.resourceType = jTk["type"].ToString();
                    rmResource.resourceLocation = jTk["location"].ToString();
                    rmResourceOper.InsertEntity(rmResource);

                    jTk = jTk.Next;
        }

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }  
        }
Example #2
0
 public bool InsertEntity(RMResourceEntity rmResource)
 {
     var operation = TableOperation.InsertOrReplace(rmResource);
     _table.Execute(operation);
     return true;
 }