| 806 | } |
| 807 | |
| 808 | func UpdateCommonVariableValue(opts *UpdateOptions, vars []variables.TenantCommonVariable, missingVars []variables.TenantCommonVariable, environmentMap map[string]string) (bool, []variables.TenantCommonVariablePayload, error) { |
| 809 | var environmentIds []string |
| 810 | for id, environment := range environmentMap { |
| 811 | for _, optEnvironment := range opts.Environments.Value { |
| 812 | if strings.EqualFold(environment, optEnvironment) || strings.EqualFold(id, optEnvironment) { |
| 813 | environmentIds = append(environmentIds, id) |
| 814 | } |
| 815 | } |
| 816 | } |
| 817 | |
| 818 | var variablesWithMatchingVariableSet []variables.TenantCommonVariable |
| 819 | for _, v := range vars { |
| 820 | if strings.EqualFold(v.LibraryVariableSetName, opts.LibraryVariableSet.Value) { |
| 821 | variablesWithMatchingVariableSet = append(variablesWithMatchingVariableSet, v) |
| 822 | } |
| 823 | } |
| 824 | |
| 825 | var variablesWithMatchingTemplate []variables.TenantCommonVariable |
| 826 | for _, v := range variablesWithMatchingVariableSet { |
| 827 | if strings.EqualFold(v.Template.Name, opts.Name.Value) { |
| 828 | variablesWithMatchingTemplate = append(variablesWithMatchingTemplate, v) |
| 829 | } |
| 830 | } |
| 831 | |
| 832 | var variablesWithMatchingScope []variables.TenantCommonVariable |
| 833 | for _, v := range variablesWithMatchingTemplate { |
| 834 | if util.SliceEquals(v.Scope.EnvironmentIds, environmentIds) { |
| 835 | variablesWithMatchingScope = append(variablesWithMatchingScope, v) |
| 836 | } |
| 837 | } |
| 838 | |
| 839 | var matchingMissingVariables []variables.TenantCommonVariable |
| 840 | |
| 841 | if len(variablesWithMatchingScope) == 0 { |
| 842 | if len(missingVars) > 0 { |
| 843 | var missingVariablesWithMatchingTemplate []variables.TenantCommonVariable |
| 844 | for _, v := range missingVars { |
| 845 | if strings.EqualFold(v.LibraryVariableSetName, opts.LibraryVariableSet.Value) && strings.EqualFold(v.Template.Name, opts.Name.Value) { |
| 846 | missingVariablesWithMatchingTemplate = append(missingVariablesWithMatchingTemplate, v) |
| 847 | } |
| 848 | } |
| 849 | |
| 850 | for _, v := range missingVariablesWithMatchingTemplate { |
| 851 | if util.SliceContainsSlice(v.Scope.EnvironmentIds, environmentIds) { |
| 852 | variablesWithMatchingTemplate = append(variablesWithMatchingTemplate, missingVariablesWithMatchingTemplate[0]) |
| 853 | matchingMissingVariables = append(matchingMissingVariables, missingVariablesWithMatchingTemplate[0]) |
| 854 | } |
| 855 | } |
| 856 | } |
| 857 | |
| 858 | if len(variablesWithMatchingTemplate) > 0 && len(matchingMissingVariables) == 0 { |
| 859 | if len(opts.Environments.Value) == 0 { |
| 860 | matchingMissingVariables = append(matchingMissingVariables, variablesWithMatchingTemplate[0]) |
| 861 | } else { |
| 862 | environmentsWithExistingValues := make(map[string]bool) |
| 863 | |
| 864 | for _, v := range variablesWithMatchingTemplate { |
| 865 | for _, e := range v.Scope.EnvironmentIds { |