// updates the local assets, either removing (if replace = false) or adding/replacing (if replace = true) the specified assets
        private static void Set(String workspacePath, ICollection <AutomationAsset> assetsToAffect, bool replace, String encryptionCertThumbprint)
        {
            LocalAssets localAssets = LocalAssetsStore.Get(workspacePath, encryptionCertThumbprint);

            foreach (var assetToAffect in assetsToAffect)
            {
                AssetJson assetToDelete = LocalAssetsStore.FindMatchingLocalAsset(localAssets, assetToAffect);

                if (assetToAffect is AutomationVariable)
                {
                    var variableToAffect = (AutomationVariable)assetToAffect;

                    if (assetToDelete != null)
                    {
                        var variableToDelete = (VariableJson)assetToDelete;

                        // Encrypted variable assets returned from the cloud have their values removed,
                        // so keep the old local asset value instead of overwriting the local asset value with null
                        if (variableToAffect.Encrypted && variableToAffect.getValue() == null)
                        {
                            variableToAffect.setValue(variableToDelete.Value);
                        }

                        localAssets.Variables.Remove(variableToDelete);
                    }

                    if (replace)
                    {
                        localAssets.Variables.Add(new VariableJson(variableToAffect));
                    }
                }

                else if (assetToAffect is AutomationCredential)
                {
                    var credToAffect = (AutomationCredential)assetToAffect;

                    if (assetToDelete != null)
                    {
                        var credToDelete = (CredentialJson)assetToDelete;

                        // PSCredential assets returned from the cloud have their passwords removed,
                        // so keep the old local asset password instead of overwriting the local asset password with null
                        if (credToAffect.getPassword() == null)
                        {
                            credToAffect.setPassword(credToDelete.Password);
                        }

                        localAssets.PSCredentials.Remove(credToDelete);
                    }

                    if (replace)
                    {
                        localAssets.PSCredentials.Add(new CredentialJson(credToAffect));
                    }
                }

                else if (assetToAffect is AutomationConnection)
                {
                    // TODO: support carry over of null fields from previous local asset

                    if (assetToDelete != null)
                    {
                        localAssets.Connections.Remove((ConnectionJson)assetToDelete);
                    }

                    if (replace)
                    {
                        localAssets.Connections.Add(new ConnectionJson((AutomationConnection)assetToAffect));
                    }
                }
            }

            DirectoryInfo dir = Directory.CreateDirectory(workspacePath);

            UnsecureLocalAssetsContainerJson.Set(workspacePath, localAssets);
            SecureLocalAssetsContainerJson.Set(workspacePath, localAssets, encryptionCertThumbprint);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="AutomationAsset"/> class.
 /// </summary>
 public AutomationAsset(AssetJson localJson, DateTime? lastModifiedCloud)
     : base(localJson.Name, DateTime.Parse(localJson.LastModified, null, DateTimeStyles.RoundtripKind), lastModifiedCloud)
 {
     this.ValueFields = new Dictionary<string, Object>();
 }
        // updates the local assets, either removing (if replace = false) or adding/replacing (if replace = true) the specified assets
        private static void Set(String workspacePath, ICollection <AutomationAsset> assetsToAffect, bool replace, String encryptionCertThumbprint, ICollection <ConnectionType> connectionTypes)
        {
            LocalAssets localAssets = LocalAssetsStore.Get(workspacePath, encryptionCertThumbprint, connectionTypes);

            foreach (var assetToAffect in assetsToAffect)
            {
                AssetJson assetToDelete = LocalAssetsStore.FindMatchingLocalAsset(localAssets, assetToAffect);

                if (assetToAffect is AutomationVariable)
                {
                    var variableToAffect = (AutomationVariable)assetToAffect;

                    if (assetToDelete != null)
                    {
                        var variableToDelete = (VariableJson)assetToDelete;

                        // Encrypted variable assets returned from the cloud have their values removed,
                        // so keep the old local asset value instead of overwriting the local asset value with null
                        if (variableToAffect.Encrypted && variableToAffect.getValue() == null)
                        {
                            variableToAffect.setValue(variableToDelete.Value);
                        }

                        localAssets.Variables.Remove(variableToDelete);
                    }

                    if (replace)
                    {
                        localAssets.Variables.Add(new VariableJson(variableToAffect));
                    }
                }

                else if (assetToAffect is AutomationCredential)
                {
                    var credToAffect = (AutomationCredential)assetToAffect;

                    if (assetToDelete != null)
                    {
                        var credToDelete = (CredentialJson)assetToDelete;

                        // PSCredential assets returned from the cloud have their passwords removed,
                        // so keep the old local asset password instead of overwriting the local asset password with null
                        if (credToAffect.getPassword() == null)
                        {
                            credToAffect.setPassword(credToDelete.Password);
                        }

                        localAssets.PSCredentials.Remove(credToDelete);
                    }

                    if (replace)
                    {
                        localAssets.PSCredentials.Add(new CredentialJson(credToAffect));
                    }
                }

                else if (assetToAffect is AutomationConnection)
                {
                    var connectionToAffect = (AutomationConnection)assetToAffect;

                    if (assetToDelete != null)
                    {
                        var connectionToDelete = (ConnectionJson)assetToDelete;

                        IDictionary <string, FieldDefinition> connectionFieldDefinitions = new Dictionary <string, FieldDefinition>();
                        if (connectionTypes != null)
                        {
                            foreach (var connectionType in connectionTypes)
                            {
                                if (connectionType.Name.Equals(connectionToAffect.ConnectionType))
                                {
                                    connectionFieldDefinitions = connectionType.Properties.FieldDefinitions;
                                    break;
                                }
                            }
                        }

                        // Connection assets returned from the cloud have null values for their encrypted fields,
                        // so keep the old local asset encrypted field values instead of overwriting the local asset encrypted field values with null
                        foreach (var valueField in connectionToDelete.ValueFields)
                        {
                            if (connectionToAffect.getFields()[valueField.Key] == null && connectionFieldDefinitions[valueField.Key].IsEncrypted)
                            {
                                connectionToAffect.getFields()[valueField.Key] = valueField.Value;
                            }
                        }

                        localAssets.Connections.Remove(connectionToDelete);
                    }

                    if (replace)
                    {
                        localAssets.Connections.Add(new ConnectionJson(connectionToAffect));
                    }
                }
                else if (assetToAffect is AutomationCertificate)
                {
                    var certToAffect = (AutomationCertificate)assetToAffect;

                    if (assetToDelete != null)
                    {
                        var certToDelete = (CertificateJson)assetToDelete;

                        // Certificate assets returned from the cloud have their passwords removed,
                        // so keep the old local asset password instead of overwriting the local asset password with null
                        if (certToAffect.getCertPath() == null)
                        {
                            certToAffect.setPassword(certToDelete.Password);
                            certToAffect.setCertPath(certToDelete.CertPath);
                        }

                        localAssets.Certificate.Remove(certToDelete);
                    }

                    if (replace)
                    {
                        localAssets.Certificate.Add(new CertificateJson(certToAffect));
                    }
                }
            }

            DirectoryInfo dir = Directory.CreateDirectory(workspacePath);

            UnsecureLocalAssetsContainerJson.Set(workspacePath, localAssets);
            SecureLocalAssetsContainerJson.Set(workspacePath, localAssets, encryptionCertThumbprint, connectionTypes);
        }
Exemple #4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="AutomationAsset"/> class.
 /// </summary>
 public AutomationAsset(AssetJson localJson, DateTime?lastModifiedCloud) :
     base(localJson.Name, DateTime.Parse(localJson.LastModified, null, DateTimeStyles.RoundtripKind), lastModifiedCloud)
 {
     this.ValueFields = new Dictionary <string, Object>();
 }