Beispiel #1
0
        private static ScopeSpecification CreateScopeSpesification(OctoVariable variable, VariableSetResource variableSet)
        {
            var scopeSpecifiaciton = new ScopeSpecification();

            variable.Scopes.ForEach(scope =>
            {
                ScopeField scopeName = FindScopeName(scope);

                List <ReferenceDataItem> referenceDataItems = FindScopeValue(scopeName, variableSet);

                List <string> scopeValues = referenceDataItems.Join(scope.Values,
                                                                    refDataItem => refDataItem.Name,
                                                                    selectedScope => selectedScope,
                                                                    (item, s) => item.Id)
                                            .ToList();

                if (!scopeValues.Any())
                {
                    throw new CakeException($"({string.Join(",", scope.Values)}) value(s) can not be found on ({scope.Name}) scope.");
                }

                var value = new ScopeValue(scopeValues.First(), scopeValues.Skip(1).ToArray());

                scopeSpecifiaciton.Add(scopeName, value);
            });

            return(scopeSpecifiaciton);
        }
Beispiel #2
0
        public async Task UpdateVariableSet(VariableSet varSet)
        {
            var id = varSet.Id;

            if (varSet.IdType == VariableSet.VariableIdTypes.Library)
            {
                id = (await client.Repository.LibraryVariableSets.Get(id)).VariableSetId;
            }
            var set = await client.Repository.VariableSets.Get(id);

            foreach (var variable in varSet.Variables)
            {
                var scope = new ScopeSpecification();

                if (variable.EnvironmentIds.Any())
                {
                    scope.Add(ScopeField.Environment, new ScopeValue(variable.EnvironmentIds));
                }
                if (variable.TargetIds.Any())
                {
                    scope.Add(ScopeField.Machine, new ScopeValue(variable.TargetIds));
                }
                if (variable.RoleIds.Any())
                {
                    scope.Add(ScopeField.Role, new ScopeValue(variable.RoleIds));
                }

                set.AddOrUpdateVariableValue(variable.Key, variable.Value, scope);
            }

            await client.Repository.VariableSets.Modify(set);
        }
        private FriendlyScopeCollection ToFriendlyScopeCollection(ScopeSpecification variableScope, VariableScopeValues scopeValues)
        {
            var scopeCollection = new FriendlyScopeCollection();

            foreach (var scope in variableScope)
            {
                switch (scope.Key.ToString())
                {
                case "Machine":
                    scopeCollection.Machines = scopeValues.Machines.Where(m => scope.Value.Contains(m.Id)).Select(m => m.Name).ToList();
                    break;

                case "Environment":
                    scopeCollection.Environments = scopeValues.Environments.Where(e => scope.Value.Contains(e.Id)).Select(e => e.Name).ToList();
                    break;

                case "Action":
                    scopeCollection.Actions = scopeValues.Actions.Where(a => scope.Value.Contains(a.Id)).Select(a => a.Name).ToList();
                    break;

                case "Role":
                    scopeCollection.Roles = scopeValues.Roles.Where(r => scope.Value.Contains(r.Id)).Select(r => r.Name).ToList();
                    break;

                case "Channel":
                    scopeCollection.Channels = scopeValues.Channels.Where(c => scope.Value.Contains(c.Id)).Select(c => c.Name).ToList();
                    break;
                }
            }

            return(scopeCollection);
        }
        public async Task UpdateVars(List <SecretVariable> vars, string libraryName, IEnumerable <string> environments, IEnumerable <string> roles, bool apply)
        {
            var lib = await _octopusRepository.ValidateLibrary(libraryName).ConfigureAwait(false);

            var set = await _octopusRepository.VariableSets.Get(lib.VariableSetId).ConfigureAwait(false);

            var scope = new ScopeSpecification();

            foreach (var environment in environments)
            {
                var enviro = await _octopusRepository.ValidateEnvironment(environment).ConfigureAwait(false);

                if (scope.ContainsKey(ScopeField.Environment))
                {
                    scope[ScopeField.Environment].Add(enviro.Id);
                }
                else
                {
                    scope.Add(ScopeField.Environment, new ScopeValue(enviro.Id));
                }
            }

            await _octopusRepository.ValidateRoles(roles).ConfigureAwait(false);

            scope.Add(ScopeField.Role, new ScopeValue(roles));
            foreach (var variable in vars)
            {
                set.AddOrUpdateVariableValue(variable.Name, variable.Value, scope, variable.IsSecret);
            }
            if (apply)
            {
                await _octopusRepository.VariableSets.Modify(set).ConfigureAwait(false);
            }
        }
Beispiel #5
0
 public static ScopeSpecification UpdateWith(this ScopeSpecification resource, IReadOnlyDictionary <VariableScopeType, IEnumerable <ElementReference> > model, IOctopusRepository repository, DeploymentProcessResource deploymentProcess, ProjectResource project)
 {
     resource.Clear();
     foreach (var kv in model)
     {
         resource.Add((ScopeField)kv.Key, new ScopeValue(kv.Value.Select(reference => ResolveId(kv.Key, reference, repository, deploymentProcess, project))));
     }
     return(resource);
 }
Beispiel #6
0
        private static ScopeSpecification CreateScope(ScopeSpecification scopeSpec, CopyScopeValue copyAction = null)
        {
            var spec = new ScopeSpecification();

            foreach (var scope in scopeSpec)
            {
                if (scope.Key != ScopeField.Action)
                    spec.Add(scope.Key, new ScopeValue(scope.Value));
                else if (copyAction != null)
                    spec.Add(scope.Key, copyAction(scope));
            }

            return spec;
        }
        private VariableResource CreateVariable(string key, VariableSetResource variableSet)
        {
            var scope = new ScopeSpecification();
            scope.AddRange(VariableFilter.BuildScopeMap(variableSet.ScopeValues, _config.Filter));

            var v = new VariableResource
            {
                Name = key,
                Scope = scope
            };

            variableSet.Variables.Add(v);

            return v;
        }
        private static ScopeSpecification CreateScope(ScopeSpecification scopeSpec, CopyScopeValue copyAction = null)
        {
            var spec = new ScopeSpecification();

            foreach (var scope in scopeSpec)
            {
                if (scope.Key != ScopeField.Action)
                {
                    spec.Add(scope.Key, new ScopeValue(scope.Value));
                }
                else if (copyAction != null)
                {
                    spec.Add(scope.Key, copyAction(scope));
                }
            }

            return(spec);
        }
        public VariableSetResource AddOrUpdateVariableValue(string name, string value, ScopeSpecification scope)
        {
            var existing = Variables.FirstOrDefault(x => string.Equals(x.Name, name, StringComparison.OrdinalIgnoreCase) && x.Scope.Equals(scope));
            if (existing == null)
            {
                var template = new VariableResource
                {
                    Name = name,
                    Value = value,
                    Scope = scope,
                };

                Variables.Add(template);
            }
            else
            {
                existing.Name = name;
                existing.Value = value;
                existing.Scope = scope;
            }

            return this;
        }
Beispiel #10
0
 public VariableSetEditor AddOrUpdateVariableValue(string name, string value, ScopeSpecification scope)
 {
     Instance.AddOrUpdateVariableValue(name, value, scope);
     return(this);
 }
Beispiel #11
0
 public static Dictionary <VariableScopeType, IEnumerable <ElementReference> > ToModel(this ScopeSpecification resource, DeploymentProcessResource deploymentProcessResource, IOctopusRepository repository)
 {
     return(resource.ToDictionary(kv => (VariableScopeType)kv.Key,
                                  kv => kv.Value.Select(id => ResolveReference(kv.Key, id, repository, deploymentProcessResource)).ToArray().AsEnumerable()));
 }
Beispiel #12
0
 public VariableSetEditor AddOrUpdateVariableValue(string name, string value, ScopeSpecification scope, bool isSensitive, string description)
 {
     Instance.AddOrUpdateVariableValue(name, value, scope, isSensitive, description);
     return(this);
 }
Beispiel #13
0
        public static async Task <Dictionary <VariableScopeType, IEnumerable <ElementReference> > > ToModel(this ScopeSpecification resource, DeploymentProcessResource deploymentProcessResource, IOctopusAsyncRepository repository)
        {
            var model = new Dictionary <VariableScopeType, IEnumerable <ElementReference> >();

            foreach (var kv in resource)
            {
                model.Add((VariableScopeType)kv.Key, await Task.WhenAll(kv.Value.Select(id => ResolveReference(kv.Key, id, repository, deploymentProcessResource))));
            }

            return(await Task.FromResult(model));
        }
        private static void AddVariableToProject(string variableName, string variableValue, ProjectResource project, ScopeSpecification scope)
        {
            var variableSet = _repository.VariableSets.Get(project.VariableSetId).Result;

            variableSet.AddOrUpdateVariableValue(variableName, variableValue, scope);

            Log.Information($"Adding variable [{variableName}] to project [{project.Name}]");
            _repository.VariableSets.Modify(variableSet).Wait();
        }
 public VariableResource()
 {
     Id = Guid.NewGuid().ToString();
     Scope = new ScopeSpecification();
 }