private static AssetJson FindMatchingLocalAsset(LocalAssets localAssets, AutomationAsset asset)
        {
            List <AssetJson> assetJsonList = new List <AssetJson>();

            if (asset is AutomationVariable)
            {
                assetJsonList.AddRange(localAssets.Variables);
            }
            else if (asset is AutomationCredential)
            {
                assetJsonList.AddRange(localAssets.PSCredentials);
            }
            else if (asset is AutomationConnection)
            {
                assetJsonList.AddRange(localAssets.Connections);
            }

            foreach (var currentLocalAsset in assetJsonList)
            {
                if (asset.Name == currentLocalAsset.Name)
                {
                    return(currentLocalAsset);
                }
            }

            return(null);
        }
        private static AssetJson FindMatchingLocalAsset(LocalAssets localAssets, AutomationAsset asset)
        {
            List<AssetJson> assetJsonList = new List<AssetJson>();
            
            if (asset is AutomationVariable)
            {
                assetJsonList.AddRange(localAssets.Variables);
            }
            else if (asset is AutomationCredential)
            {
                assetJsonList.AddRange(localAssets.PSCredentials);
            }
            else if (asset is AutomationConnection)
            {
                assetJsonList.AddRange(localAssets.Connections);
            }

            foreach (var currentLocalAsset in assetJsonList)
            {
                if (asset.Name == currentLocalAsset.Name)
                {
                    return currentLocalAsset;
                }
            }

            return null;
        }
        public AssetJson(AutomationAsset asset)
        {
            this.Name = asset.Name;

            if(asset.LastModifiedCloud == null)
            {
                setLastModified((System.DateTime)asset.LastModifiedLocal);
            }
            else if (asset.LastModifiedLocal == null)
            {
                setLastModified((System.DateTime)asset.LastModifiedCloud);
            }
            else
            {
                var lastModifiedDatetime = (System.DateTime)(asset.LastModifiedLocal > asset.LastModifiedCloud ? asset.LastModifiedLocal : asset.LastModifiedCloud);
                setLastModified(lastModifiedDatetime);
            }
        }
예제 #4
0
        public AssetJson(AutomationAsset asset)
        {
            this.Name = asset.Name;

            if (asset.LastModifiedCloud == null)
            {
                setLastModified((System.DateTime)asset.LastModifiedLocal);
            }
            else if (asset.LastModifiedLocal == null)
            {
                setLastModified((System.DateTime)asset.LastModifiedCloud);
            }
            else
            {
                var lastModifiedDatetime = (System.DateTime)(asset.LastModifiedLocal > asset.LastModifiedCloud ? asset.LastModifiedLocal : asset.LastModifiedCloud);
                setLastModified(lastModifiedDatetime);
            }
        }
        /// <summary>
        /// Returns if the asset exists locally or in the cloud
        /// </summary>
        /// <param name="assetName"></param>
        /// <param name="assetType"></param>
        /// <param name="localWorkspacePath"></param>
        /// <param name="automationApi"></param>
        /// <param name="resourceGroupName"></param>
        /// <param name="automationAccountName"></param>
        /// <param name="encryptionCertThumbprint"></param>
        /// <returns>null if the asset does not exist or else returns the asset</returns>
        public static async Task <AutomationAsset> GetAsset(String assetName, String assetType, String localWorkspacePath, AutomationManagementClient automationApi, string resourceGroupName, string automationAccountName, string encryptionCertThumbprint, ICollection <ConnectionType> connectionTypes)
        {
            AutomationAsset automationAsset = null;

            // Get local assets
            LocalAssets localAssets = LocalAssetsStore.Get(localWorkspacePath, encryptionCertThumbprint, connectionTypes);

            // Search for variables
            CancellationTokenSource cts = new CancellationTokenSource();

            cts.CancelAfter(TIMEOUT_MS);
            if (assetType == Constants.AssetType.Variable)
            {
                // Check local asset store first
                var localVariable = localAssets.Variables.Find(asset => asset.Name == assetName);
                if (localVariable != null)
                {
                    automationAsset = new AutomationVariable(localVariable);
                }
                else
                {
                    try
                    {
                        // Check cloud. Catch exception if it doesn't exist
                        VariableGetResponse cloudVariable = await automationApi.Variables.GetAsync(resourceGroupName, automationAccountName, assetName, cts.Token);

                        automationAsset = new AutomationVariable(cloudVariable.Variable);
                    }
                    catch (Exception e)
                    {
                        // If the exception is not found, don't throw new exception as this is expected
                        if (e.HResult != -2146233088)
                        {
                            throw e;
                        }
                    }
                }
            }
            // Search for credentials
            else if (assetType == Constants.AssetType.Credential)
            {
                // Check local asset store first
                var localCredential = localAssets.PSCredentials.Find(asset => asset.Name == assetName);
                if (localCredential != null)
                {
                    automationAsset = new AutomationCredential(localCredential);
                }
                else
                {
                    try
                    {
                        // Check cloud. Catch execption if it doesn't exist
                        CredentialGetResponse cloudVariable = await automationApi.PsCredentials.GetAsync(resourceGroupName, automationAccountName, assetName, cts.Token);

                        automationAsset = new AutomationCredential(cloudVariable.Credential);
                    }
                    catch (Exception e)
                    {
                        // If the exception is not found, don't throw new exception as this is expected
                        if (e.HResult != -2146233088)
                        {
                            throw e;
                        }
                    }
                }
            }
            // Search for connections
            else if (assetType == Constants.AssetType.Connection)
            {
                // Check local asset store first
                var localConnection = localAssets.Connections.Find(asset => asset.Name == assetName);
                if (localConnection != null)
                {
                    automationAsset = new AutomationConnection(localConnection);
                }
                else
                {
                    try
                    {
                        // Check cloud. Catch exception if it doesn't exist
                        ConnectionGetResponse cloudConnection = await automationApi.Connections.GetAsync(resourceGroupName, automationAccountName, assetName, cts.Token);

                        cts = new CancellationTokenSource();
                        cts.CancelAfter(TIMEOUT_MS);
                        ConnectionTypeGetResponse connectionType = await automationApi.ConnectionTypes.GetAsync(resourceGroupName, automationAccountName,
                                                                                                                cloudConnection.Connection.Properties.ConnectionType.Name, cts.Token);

                        automationAsset = new AutomationConnection(cloudConnection.Connection, connectionType.ConnectionType);
                    }
                    catch (Exception e)
                    {
                        // If the exception is not found, don't throw new exception as this is expected
                        if (e.HResult != -2146233088)
                        {
                            throw e;
                        }
                    }
                }
            }
            // Search for certificates
            else if (assetType == Constants.AssetType.Certificate)
            {
                // Check local asset store first
                var localCertificate = localAssets.Certificate.Find(asset => asset.Name == assetName);
                if (localCertificate != null)
                {
                    automationAsset = new AutomationCertificate(localCertificate);
                }
                else
                {
                    try
                    {
                        // Check cloud. Catch execption if it doesn't exist
                        CertificateGetResponse cloudCertificate = await automationApi.Certificates.GetAsync(resourceGroupName, automationAccountName, assetName, cts.Token);

                        automationAsset = new AutomationCertificate(cloudCertificate.Certificate);
                    }
                    catch (Exception e)
                    {
                        // If the exception is not found, don't throw new exception as this is expected
                        if (e.HResult != -2146233088)
                        {
                            throw e;
                        }
                    }
                }
            }
            return(automationAsset);
        }