/// <summary>
 /// Checks whether resource exists.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.Resources.IResourceOperations.
 /// </param>
 /// <param name='resourceGroupName'>
 /// Required. The name of the resource group. The name is case
 /// insensitive.
 /// </param>
 /// <param name='identity'>
 /// Required. Resource identity.
 /// </param>
 /// <returns>
 /// Resource group information.
 /// </returns>
 public static ResourceExistsResult CheckExistence(this IResourceOperations operations, string resourceGroupName, ResourceIdentity identity)
 {
     return Task.Factory.StartNew((object s) => 
     {
         return ((IResourceOperations)s).CheckExistenceAsync(resourceGroupName, identity);
     }
     , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult();
 }
        public ResourceIdentity ToResourceIdentity()
        {
            if (string.IsNullOrEmpty(ResourceType))
            {
                throw new ArgumentNullException("ResourceType");
            }
            if (ResourceType.IndexOf('/') < 0)
            {
                throw new ArgumentException(ProjectResources.ResourceTypeFormat, "ResourceType");
            }

            ResourceIdentity identity = new ResourceIdentity
                {
                    ResourceName = Name,
                    ParentResourcePath = ParentResource,
                    ResourceProviderNamespace = ResourceIdentifier.GetProviderFromResourceType(ResourceType),
                    ResourceType = ResourceIdentifier.GetTypeFromResourceType(ResourceType),
                    ResourceProviderApiVersion = ApiVersion
                };

            return identity;
        }
        public void GetsOneResource()
        {
            FilterResourcesOptions options = new FilterResourcesOptions() { ResourceGroup = resourceGroupName, Name = resourceName };
            Resource expected = new Resource() { Id = "resourceId", Location = resourceGroupLocation, Name = resourceName };
            ResourceIdentity actualParameters = new ResourceIdentity();
            string actualResourceGroup = null;
            resourceOperationsMock.Setup(f => f.GetAsync(resourceGroupName, It.IsAny<ResourceIdentity>(), new CancellationToken()))
                .Returns(Task.Factory.StartNew(() => new ResourceGetResult
                {
                    Resource = expected
                }))
                .Callback((string rg, ResourceIdentity p, CancellationToken ct) => { actualParameters = p; actualResourceGroup = rg; });

            List<Resource> result = resourcesClient.FilterResources(options);

            Assert.Equal(1, result.Count);
            Assert.Equal(options.Name, result.First().Name);
            Assert.Equal(expected.Id, result.First().Id);
            Assert.Equal(expected.Location, result.First().Location);
            Assert.Equal(expected.Name, actualParameters.ResourceName);
            Assert.Equal(resourceGroupName, actualResourceGroup);
        }
        public ResourceClientTests()
        {
            resourceManagementClientMock = new Mock<IResourceManagementClient>();
            authorizationManagementClientMock = new Mock<IAuthorizationManagementClient>();
            deploymentsMock = new Mock<IDeploymentOperations>();
            resourceGroupMock = new Mock<IResourceGroupOperations>();
            resourceOperationsMock = new Mock<IResourceOperations>();
            galleryTemplatesClientMock = new Mock<GalleryTemplatesClient>();
            eventsClientMock = new Mock<IEventsClient>();
            deploymentOperationsMock = new Mock<IDeploymentOperationOperations>();
            eventDataOperationsMock = new Mock<IEventDataOperations>();
            providersMock = new Mock<IProviderOperations>();
            providersMock.Setup(f => f.ListAsync(null, new CancellationToken()))
                .Returns(Task.Factory.StartNew(() => new ProviderListResult
                {
                    Providers = new List<Provider>()
                }));
            progressLoggerMock = new Mock<Action<string>>();
            errorLoggerMock = new Mock<Action<string>>();
            permissionOperationsMock = new Mock<IPermissionOperations>();
            resourceManagementClientMock.Setup(f => f.Deployments).Returns(deploymentsMock.Object);
            resourceManagementClientMock.Setup(f => f.ResourceGroups).Returns(resourceGroupMock.Object);
            resourceManagementClientMock.Setup(f => f.Resources).Returns(resourceOperationsMock.Object);
            resourceManagementClientMock.Setup(f => f.DeploymentOperations).Returns(deploymentOperationsMock.Object);
            resourceManagementClientMock.Setup(f => f.Providers).Returns(providersMock.Object);
            eventsClientMock.Setup(f => f.EventData).Returns(eventDataOperationsMock.Object);
            authorizationManagementClientMock.Setup(f => f.Permissions).Returns(permissionOperationsMock.Object);
            resourcesClient = new ResourcesClient(
                resourceManagementClientMock.Object,
                galleryTemplatesClientMock.Object,
                eventsClientMock.Object,
                authorizationManagementClientMock.Object)
                {
                    VerboseLogger = progressLoggerMock.Object,
                    ErrorLogger = errorLoggerMock.Object
                };

            resourceIdentity = new ResourceIdentity
            {
                ParentResourcePath = "sites/siteA",
                ResourceName = "myResource",
                ResourceProviderNamespace = "Microsoft.Web",
                ResourceType = "sites"
            };
            properties = new Dictionary<string, object>
                {
                    {"name", "site1"},
                    {"siteMode", "Standard"},
                    {"computeMode", "Dedicated"},
                    {"misc", new Dictionary<string, object>
                        {
                            {"key1", "value1"},
                            {"key2", "value2"}
                        }}
                };
            serializedProperties = JsonConvert.SerializeObject(properties, new JsonSerializerSettings
            {
                TypeNameAssemblyFormat = FormatterAssemblyStyle.Simple,
                TypeNameHandling = TypeNameHandling.None
            });

            sampleEvents = new List<EventData>();
            sampleEvents.Add(new EventData
                {
                    EventDataId = "ac7d2ab5-698a-4c33-9c19-0a93d3d7f527",
                    EventName = new LocalizableString { LocalizedValue = "Start request" },
                    EventSource = new LocalizableString { LocalizedValue = "Microsoft Resources" },
                    EventChannels = EventChannels.Operation,
                    Level = EventLevel.Informational,
                    EventTimestamp = DateTime.Now,
                    OperationId = "c0f2e85f-efb0-47d0-bf90-f983ec8be91d",
                    SubscriptionId = "c0f2e85f-efb0-47d0-bf90-f983ec8be91d",
                    CorrelationId = "c0f2e85f-efb0-47d0-bf90-f983ec8be91d",
                    OperationName =
                        new LocalizableString
                            {
                                LocalizedValue = "Microsoft.Resources/subscriptions/resourcegroups/deployments/write"
                            },
                    Status = new LocalizableString { LocalizedValue = "Succeeded" },
                    SubStatus = new LocalizableString { LocalizedValue = "Created" },
                    ResourceGroupName = "foo",
                    ResourceProviderName = new LocalizableString { LocalizedValue = "Microsoft Resources" },
                    ResourceUri =
                        "/subscriptions/ffce8037-a374-48bf-901d-dac4e3ea8c09/resourcegroups/foo/deployments/testdeploy",
                    HttpRequest = new HttpRequestInfo
                        {
                            Uri =
                                "http://path/subscriptions/ffce8037-a374-48bf-901d-dac4e3ea8c09/resourcegroups/foo/deployments/testdeploy",
                            Method = "PUT",
                            ClientRequestId = "1234",
                            ClientIpAddress = "123.123.123.123"
                        },
                    Authorization = new SenderAuthorization
                        {
                            Action = "PUT",
                            Condition = "",
                            Role = "Sender",
                            Scope = "None"
                        },
                    Claims = new Dictionary<string, string>
                        {
                            {"aud", "https://management.core.windows.net/"},
                            {"iss", "https://sts.windows.net/123456/"},
                            {"iat", "h123445"},
                            {"http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name", "*****@*****.**"}
                        },
                    Properties = new Dictionary<string, string>()
                });
            sampleEvents.Add(new EventData
            {
                EventDataId = "ac7d2ab5-698a-4c33-9c19-0sdfsdf34r54",
                EventName = new LocalizableString { LocalizedValue = "End request" },
                EventSource = new LocalizableString { LocalizedValue = "Microsoft Resources" },
                EventChannels = EventChannels.Operation,
                Level = EventLevel.Informational,
                EventTimestamp = DateTime.Now,
                OperationId = "c0f2e85f-efb0-47d0-bf90-f983ec8be91d",
                OperationName =
                    new LocalizableString
                    {
                        LocalizedValue = "Microsoft.Resources/subscriptions/resourcegroups/deployments/write"
                    },
                Status = new LocalizableString { LocalizedValue = "Succeeded" },
                SubStatus = new LocalizableString { LocalizedValue = "Created" },
                ResourceGroupName = "foo",
                ResourceProviderName = new LocalizableString { LocalizedValue = "Microsoft Resources" },
                ResourceUri =
                    "/subscriptions/ffce8037-a374-48bf-901d-dac4e3ea8c09/resourcegroups/foo/deployments/testdeploy",
                HttpRequest = new HttpRequestInfo
                {
                    Uri =
                        "http://path/subscriptions/ffce8037-a374-48bf-901d-dac4e3ea8c09/resourcegroups/foo/deployments/testdeploy",
                    Method = "PUT",
                    ClientRequestId = "1234",
                    ClientIpAddress = "123.123.123.123"
                },
                Authorization = new SenderAuthorization
                {
                    Action = "PUT",
                    Condition = "",
                    Role = "Sender",
                    Scope = "None"
                },
                Claims = new Dictionary<string, string>
                        {
                            {"aud", "https://management.core.windows.net/"},
                            {"iss", "https://sts.windows.net/123456/"},
                            {"iat", "h123445"}
                        },
                Properties = new Dictionary<string, string>()
            });
        }
 /// <summary>
 /// Checks whether resource exists.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.Resources.IResourceOperations.
 /// </param>
 /// <param name='resourceGroupName'>
 /// Required. The name of the resource group. The name is case
 /// insensitive.
 /// </param>
 /// <param name='identity'>
 /// Required. Resource identity.
 /// </param>
 /// <returns>
 /// Resource group information.
 /// </returns>
 public static Task<ResourceExistsResult> CheckExistenceAsync(this IResourceOperations operations, string resourceGroupName, ResourceIdentity identity)
 {
     return operations.CheckExistenceAsync(resourceGroupName, identity, CancellationToken.None);
 }
 /// <summary>
 /// Gets a resource permissions.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.Resources.IResourceOperations.
 /// </param>
 /// <param name='resourceGroupName'>
 /// Required. The name of the resource group. The name is case
 /// insensitive.
 /// </param>
 /// <param name='identity'>
 /// Required. Resource identity.
 /// </param>
 /// <returns>
 /// Resource group permissions information.
 /// </returns>
 public static Task<PermissionGetResult> GetPermissionsAsync(this IResourceOperations operations, string resourceGroupName, ResourceIdentity identity)
 {
     return operations.GetPermissionsAsync(resourceGroupName, identity, CancellationToken.None);
 }
 /// <summary>
 /// Delete resource and all of its resources.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.Resources.IResourceOperations.
 /// </param>
 /// <param name='resourceGroupName'>
 /// Required. The name of the resource group. The name is case
 /// insensitive.
 /// </param>
 /// <param name='identity'>
 /// Required. Resource identity.
 /// </param>
 /// <returns>
 /// A standard service response including an HTTP status code and
 /// request ID.
 /// </returns>
 public static Task<OperationResponse> DeleteAsync(this IResourceOperations operations, string resourceGroupName, ResourceIdentity identity)
 {
     return operations.DeleteAsync(resourceGroupName, identity, CancellationToken.None);
 }
 /// <summary>
 /// Create a resource.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.Resources.IResourceOperations.
 /// </param>
 /// <param name='resourceGroupName'>
 /// Required. The name of the resource group. The name is case
 /// insensitive.
 /// </param>
 /// <param name='identity'>
 /// Required. Resource identity.
 /// </param>
 /// <param name='parameters'>
 /// Required. Create or update resource parameters.
 /// </param>
 /// <returns>
 /// Resource information.
 /// </returns>
 public static Task<ResourceCreateOrUpdateResult> CreateOrUpdateAsync(this IResourceOperations operations, string resourceGroupName, ResourceIdentity identity, BasicResource parameters)
 {
     return operations.CreateOrUpdateAsync(resourceGroupName, identity, parameters, CancellationToken.None);
 }
 /// <summary>
 /// Create a resource.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.Resources.IResourceOperations.
 /// </param>
 /// <param name='resourceGroupName'>
 /// Required. The name of the resource group. The name is case
 /// insensitive.
 /// </param>
 /// <param name='identity'>
 /// Required. Resource identity.
 /// </param>
 /// <param name='parameters'>
 /// Required. Create or update resource parameters.
 /// </param>
 /// <returns>
 /// Resource information.
 /// </returns>
 public static ResourceCreateOrUpdateResult CreateOrUpdate(this IResourceOperations operations, string resourceGroupName, ResourceIdentity identity, BasicResource parameters)
 {
     return Task.Factory.StartNew((object s) => 
     {
         return ((IResourceOperations)s).CreateOrUpdateAsync(resourceGroupName, identity, parameters);
     }
     , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult();
 }