public static async Task UploadToCloud(ICollection<AutomationAsset> assetsToUpload, AutomationManagementClient automationApi, string resourceGroupName, string automationAccountName)
        {
            var jss = new JavaScriptSerializer();
            
            foreach (var assetToUpload in assetsToUpload)
            {
                if (assetToUpload is AutomationVariable)
                {
                    var asset = (AutomationVariable)assetToUpload;

                    var properties = new VariableCreateOrUpdateProperties();
                    properties.IsEncrypted = asset.Encrypted;

                    var stringBuilder = new StringBuilder();
                    jss.Serialize(asset.getValue(), stringBuilder);
                    properties.Value = stringBuilder.ToString();
                    
                    await automationApi.Variables.CreateOrUpdateAsync(resourceGroupName, automationAccountName, new VariableCreateOrUpdateParameters(asset.Name, properties));
                }
                else if(assetToUpload is AutomationCredential)
                {
                    var asset = (AutomationCredential)assetToUpload;

                    var properties = new CredentialCreateOrUpdateProperties();
                    properties.UserName = asset.getUsername();
                    properties.Password = asset.getPassword();

                    await automationApi.PsCredentials.CreateOrUpdateAsync(resourceGroupName, automationAccountName, new CredentialCreateOrUpdateParameters(asset.Name, properties));
                }
                else if (assetToUpload is AutomationConnection)
                {
                    // TODO: implement this and certificates
                }
            }
        }
        public static async Task UploadToCloud(ICollection<AutomationAsset> assetsToUpload, AutomationManagementClient automationApi, string resourceGroupName, string automationAccountName)
        {
            var jss = new JavaScriptSerializer();

            foreach (var assetToUpload in assetsToUpload)
            {
                CancellationTokenSource cts = new CancellationTokenSource();
                cts.CancelAfter(TIMEOUT_MS);
                if (assetToUpload is AutomationVariable)
                {
                    var asset = (AutomationVariable)assetToUpload;

                    var properties = new VariableCreateOrUpdateProperties();
                    properties.IsEncrypted = asset.Encrypted;

                    var stringBuilder = new StringBuilder();
                    jss.Serialize(asset.getValue(), stringBuilder);
                    properties.Value = stringBuilder.ToString();

                    await automationApi.Variables.CreateOrUpdateAsync(resourceGroupName, automationAccountName, new VariableCreateOrUpdateParameters(asset.Name, properties), cts.Token);
                }
                else if(assetToUpload is AutomationCredential)
                {
                    var asset = (AutomationCredential)assetToUpload;

                    var properties = new CredentialCreateOrUpdateProperties();
                    properties.UserName = asset.getUsername();
                    properties.Password = asset.getPassword();

                    await automationApi.PsCredentials.CreateOrUpdateAsync(resourceGroupName, automationAccountName, new CredentialCreateOrUpdateParameters(asset.Name, properties), cts.Token);
                }
                else if (assetToUpload is AutomationConnection)
                {
                    var asset = (AutomationConnection)assetToUpload;

                    var properties = new ConnectionCreateOrUpdateProperties();
                    var connectionFieldsAsJson = new Dictionary<string, string>();

                    foreach(KeyValuePair<string, object> field in asset.getFields())
                    {
                        connectionFieldsAsJson.Add(field.Key, field.Value.ToString());
                    }

                    properties.FieldDefinitionValues = connectionFieldsAsJson;
                    properties.ConnectionType = new ConnectionTypeAssociationProperty();
                    properties.ConnectionType.Name = asset.ConnectionType;

                    await automationApi.Connections.CreateOrUpdateAsync(resourceGroupName, automationAccountName, new ConnectionCreateOrUpdateParameters(asset.Name, properties), cts.Token);
                }
                // TODO: implement certificates
            }
        }
Example #3
0
 /// <summary>
 /// Initializes a new instance of the VariableCreateOrUpdateParameters
 /// class with required arguments.
 /// </summary>
 public VariableCreateOrUpdateParameters(string name, VariableCreateOrUpdateProperties properties)
     : this()
 {
     if (name == null)
     {
         throw new ArgumentNullException("name");
     }
     if (properties == null)
     {
         throw new ArgumentNullException("properties");
     }
     this.Name       = name;
     this.Properties = properties;
 }
 /// <summary>
 /// Initializes a new instance of the VariableCreateOrUpdateParameters
 /// class with required arguments.
 /// </summary>
 public VariableCreateOrUpdateParameters(string name, VariableCreateOrUpdateProperties properties)
     : this()
 {
     if (name == null)
     {
         throw new ArgumentNullException("name");
     }
     if (properties == null)
     {
         throw new ArgumentNullException("properties");
     }
     this.Name = name;
     this.Properties = properties;
 }
        public static async Task UploadToCloud(ICollection<AutomationAsset> assetsToUpload, AutomationManagementClient automationApi, string resourceGroupName, string automationAccountName)
        {
            var jss = new JavaScriptSerializer();

            foreach (var assetToUpload in assetsToUpload)
            {
                CancellationTokenSource cts = new CancellationTokenSource();
                cts.CancelAfter(TIMEOUT_MS);
                if (assetToUpload is AutomationVariable)
                {
                    var asset = (AutomationVariable)assetToUpload;

                    var properties = new VariableCreateOrUpdateProperties();
                    properties.IsEncrypted = asset.Encrypted;

                    var stringBuilder = new StringBuilder();
                    jss.Serialize(asset.getValue(), stringBuilder);
                    properties.Value = stringBuilder.ToString();

                    await automationApi.Variables.CreateOrUpdateAsync(resourceGroupName, automationAccountName, new VariableCreateOrUpdateParameters(asset.Name, properties), cts.Token);
                }
                else if(assetToUpload is AutomationCredential)
                {
                    var asset = (AutomationCredential)assetToUpload;

                    var properties = new CredentialCreateOrUpdateProperties();
                    properties.UserName = asset.getUsername();
                    properties.Password = asset.getPassword();

                    await automationApi.PsCredentials.CreateOrUpdateAsync(resourceGroupName, automationAccountName, new CredentialCreateOrUpdateParameters(asset.Name, properties), cts.Token);
                }
                else if (assetToUpload is AutomationConnection)
                {
                    var asset = (AutomationConnection)assetToUpload;

                    var properties = new ConnectionCreateOrUpdateProperties();
                    var connectionFieldsAsJson = new Dictionary<string, string>();

                    foreach(KeyValuePair<string, object> field in asset.getFields())
                    {
                        connectionFieldsAsJson.Add(field.Key, field.Value.ToString());
                    }

                    properties.FieldDefinitionValues = connectionFieldsAsJson;
                    properties.ConnectionType = new ConnectionTypeAssociationProperty();
                    properties.ConnectionType.Name = asset.ConnectionType;

                    await automationApi.Connections.CreateOrUpdateAsync(resourceGroupName, automationAccountName, new ConnectionCreateOrUpdateParameters(asset.Name, properties), cts.Token);
                }
                else if (assetToUpload is AutomationCertificate)
                {
                    var asset = (AutomationCertificate)assetToUpload;

                    var cert = (asset.getPassword() == null)
                                ? new X509Certificate2(asset.getCertPath(), String.Empty,
                                    X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet |
                                    X509KeyStorageFlags.MachineKeySet)
                                : new X509Certificate2(asset.getCertPath(), asset.getPassword(),
                                    X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet |
                                    X509KeyStorageFlags.MachineKeySet);

                    var properties = new CertificateCreateOrUpdateProperties()
                    {
                        Base64Value = Convert.ToBase64String(cert.Export(X509ContentType.Pkcs12)),
                        Thumbprint = cert.Thumbprint,
                        IsExportable = asset.getExportable()
                    };

                    await automationApi.Certificates.CreateOrUpdateAsync(resourceGroupName, automationAccountName, new CertificateCreateOrUpdateParameters(asset.Name, properties), cts.Token);
                }
            }
        }