Best Gauge code snippet using util.removeDuplicateValues
utils.go
Source:utils.go
1// Copyright (C) 2020, 2022, Oracle and/or its affiliates.2// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.3package util4import (5 "fmt"6 "os"7 "strconv"8 "sync"9 "github.com/verrazzano/verrazzano-operator/pkg/constants"10 "github.com/verrazzano/verrazzano-operator/pkg/types"11 "go.uber.org/zap"12 apiextensionsclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"13 "k8s.io/client-go/kubernetes"14 appslistersv1 "k8s.io/client-go/listers/apps/v1"15 corelistersv1 "k8s.io/client-go/listers/core/v1"16 rbaclistersv1 "k8s.io/client-go/listers/rbac/v1"17 "k8s.io/client-go/tools/cache"18)19// LookupEnvFunc is the function used to lookup env vars - it is made available here so that tests20// can override it rather than setting env vars21var LookupEnvFunc = os.LookupEnv22// ManagedClusterConnection maintains the connection details to a ManagedCluster.23type ManagedClusterConnection struct {24 KubeClient kubernetes.Interface25 KubeExtClientSet apiextensionsclient.Interface26 Lock sync.RWMutex27 DeploymentLister appslistersv1.DeploymentLister28 DeploymentInformer cache.SharedIndexInformer29 PodLister corelistersv1.PodLister30 PodInformer cache.SharedIndexInformer31 ServiceAccountLister corelistersv1.ServiceAccountLister32 ServiceAccountInformer cache.SharedIndexInformer33 NamespaceLister corelistersv1.NamespaceLister34 NamespaceInformer cache.SharedIndexInformer35 SecretLister corelistersv1.SecretLister36 SecretInformer cache.SharedIndexInformer37 ClusterRoleLister rbaclistersv1.ClusterRoleLister38 ClusterRoleInformer cache.SharedIndexInformer39 ClusterRoleBindingLister rbaclistersv1.ClusterRoleBindingLister40 ClusterRoleBindingInformer cache.SharedIndexInformer41 ConfigMapLister corelistersv1.ConfigMapLister42 ConfigMapInformer cache.SharedIndexInformer43 DaemonSetLister appslistersv1.DaemonSetLister44 DaemonSetInformer cache.SharedIndexInformer45 ServiceLister corelistersv1.ServiceLister46 ServiceInformer cache.SharedIndexInformer47}48// DeploymentHelper defines an interface for deployments.49type DeploymentHelper interface {50 DeleteDeployment(namespace, name string) error51}52// GetManagedBindingLabels returns binding labels for managed cluster.53func GetManagedBindingLabels(binding *types.SyntheticBinding, managedClusterName string) map[string]string {54 return map[string]string{constants.K8SAppLabel: constants.VerrazzanoGroup, constants.VerrazzanoBinding: binding.Name, constants.VerrazzanoCluster: managedClusterName, constants.IstioInjection: constants.Enabled}55}56// GetManagedLabelsNoBinding return labels for managed cluster with no binding.57func GetManagedLabelsNoBinding(managedClusterName string) map[string]string {58 return map[string]string{constants.K8SAppLabel: constants.VerrazzanoGroup, constants.VerrazzanoCluster: managedClusterName}59}60// GetManagedNamespaceForBinding return the namespace for a given binding.61func GetManagedNamespaceForBinding(binding *types.SyntheticBinding) string {62 return fmt.Sprintf("%s-%s", constants.VerrazzanoPrefix, binding.Name)63}64// GetLocalBindingLabels returns binding labels for local cluster.65func GetLocalBindingLabels(binding *types.SyntheticBinding) map[string]string {66 return map[string]string{constants.K8SAppLabel: constants.VerrazzanoGroup, constants.VerrazzanoBinding: binding.Name}67}68// GetManagedClusterNamespaceForSystem returns the system namespace for Verrazzano.69func GetManagedClusterNamespaceForSystem() string {70 return constants.VerrazzanoSystem71}72// GetVmiNameForBinding returns a Verrazzano Monitoring Instance name.73func GetVmiNameForBinding(bindingName string) string {74 return bindingName75}76// GetVmiURI returns a Verrazzano Monitoring Instance URI.77func GetVmiURI(bindingName string, verrazzanoURI string) string {78 return fmt.Sprintf("vmi.%s.%s", bindingName, verrazzanoURI)79}80// GetServiceAccountNameForSystem return the system service account for Verrazzano.81func GetServiceAccountNameForSystem() string {82 return constants.VerrazzanoSystem83}84// NewVal returns an address of an int32 value.85func NewVal(value int32) *int32 {86 var val = value87 return &val88}89// New64Val returns an address of an int64 value.90func New64Val(value int64) *int64 {91 var val = value92 return &val93}94// Contains checks if a string exists in array of strings.95func Contains(s []string, e string) bool {96 for _, a := range s {97 if a == e {98 return true99 }100 }101 return false102}103// GetManagedClustersForVerrazzanoBinding returns a filtered set of only those applicable to a given104// VerrazzanoBinding given a map of available ManagedClusterConnections.105func GetManagedClustersForVerrazzanoBinding(vzSynMB *types.SyntheticModelBinding, availableManagedClusterConnections map[string]*ManagedClusterConnection) (106 map[string]*ManagedClusterConnection, error) {107 filteredManagedClusters := map[string]*ManagedClusterConnection{}108 for _, managedCluster := range vzSynMB.ManagedClusters {109 if _, ok := availableManagedClusterConnections[managedCluster.Name]; !ok {110 return nil, fmt.Errorf("Failed, managed cluster %s referenced in binding %s not found", managedCluster.Name, vzSynMB.SynBinding.Name)111 }112 filteredManagedClusters[managedCluster.Name] = availableManagedClusterConnections[managedCluster.Name]113 }114 return filteredManagedClusters, nil115}116// GetManagedClustersNotForVerrazzanoBinding returns a filtered set of those NOT applicable to a given117// VerrazzanoBinding given a map of available ManagedClusterConnections.118func GetManagedClustersNotForVerrazzanoBinding(vzSynMB *types.SyntheticModelBinding, availableManagedClusterConnections map[string]*ManagedClusterConnection) map[string]*ManagedClusterConnection {119 filteredManagedClusters := map[string]*ManagedClusterConnection{}120 for clusterName := range availableManagedClusterConnections {121 found := false122 for _, managedCluster := range vzSynMB.ManagedClusters {123 if clusterName == managedCluster.Name {124 found = true125 break126 }127 }128 if !found {129 filteredManagedClusters[clusterName] = availableManagedClusterConnections[clusterName]130 }131 }132 return filteredManagedClusters133}134// IsClusterInBinding checks if a cluster was found in a binding.135func IsClusterInBinding(clusterName string, allvzSynMBs map[string]*types.SyntheticModelBinding) bool {136 for _, mb := range allvzSynMBs {137 for _, placement := range mb.SynBinding.Spec.Placement {138 if placement.Name == clusterName {139 return true140 }141 }142 }143 return false144}145// SharedVMIDefault return true if the env var SHARED_VMI_DEFAULT is true; this may be overridden by an app binding (future)146func SharedVMIDefault() bool {147 useSharedVMI := false148 envValue, present := LookupEnvFunc(sharedVMIDefault)149 if present {150 zap.S().Debugf("Env var %s = %s", sharedVMIDefault, envValue)151 value, err := strconv.ParseBool(envValue)152 if err == nil {153 useSharedVMI = value154 } else {155 zap.S().Errorf("Failed, invalid value for %s: %s", sharedVMIDefault, envValue)156 }157 }158 return useSharedVMI159}160// GetProfileBindingName will return the binding name based on the profile161// if the profile doesn't have a special binding name, the binding name supplied is returned162// for Dev profile the VMI system binding name is returned163func GetProfileBindingName(bindingName string) string {164 if SharedVMIDefault() {165 return constants.VmiSystemBindingName166 }167 return bindingName168}169// RemoveDuplicateValues removes duplicates from a slice containing string values170func RemoveDuplicateValues(stringSlice []string) []string {171 keys := make(map[string]bool)172 var list []string173 for _, entry := range stringSlice {174 if _, value := keys[entry]; !value {175 keys[entry] = true176 list = append(list, entry)177 }178 }179 return list180}181// IsSystemProfileBindingName return true if the specified binding name is the system VMI name182func IsSystemProfileBindingName(bindingName string) bool {183 return constants.VmiSystemBindingName == bindingName184}...
fileUtils.go
Source:fileUtils.go
...173 logger.Fatalf(true, "Error getting absolute path. %v", err)174 }175 files = append(files, FindConceptFilesIn(absSpecPath)...)176 }177 return removeDuplicateValues(files)178}179func removeDuplicateValues(slice []string) []string {180 keys := make(map[string]bool)181 list := []string{}182 for _, entry := range slice {183 if _, value := keys[entry]; !value {184 keys[entry] = true185 list = append(list, entry)186 }187 }188 return list189}190// SaveFile saves contents at the given path191func SaveFile(fileName string, content string, backup bool) {192 err := common.SaveFile(fileName, content, backup)193 if err != nil {...
configmaps.go
Source:configmaps.go
1// Copyright (C) 2020, 2022, Oracle and/or its affiliates.2// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.3package local4import (5 "context"6 "reflect"7 "strings"8 "github.com/verrazzano/verrazzano-operator/pkg/assets"9 "github.com/verrazzano/verrazzano-operator/pkg/constants"10 "github.com/verrazzano/verrazzano-operator/pkg/types"11 "github.com/verrazzano/verrazzano-operator/pkg/util"12 "go.uber.org/zap"13 corev1 "k8s.io/api/core/v1"14 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"15 "k8s.io/apimachinery/pkg/labels"16 "k8s.io/client-go/kubernetes"17 corev1listers "k8s.io/client-go/listers/core/v1"18)19// UpdateConfigMaps updates config maps for a given binding in the management cluster.20func UpdateConfigMaps(binding *types.SyntheticBinding, kubeClientSet kubernetes.Interface, configMapLister corev1listers.ConfigMapLister) error {21 zap.S().Debugf("Updating Local (Management SynModel) configMaps for VerrazzanoBinding %s", binding.Name)22 // Construct the set of expected configMap - this currently consists of the ConfigMap that contains the default Grafana dashboard definitions23 newConfigMap, err := newConfigMap(binding)24 if err != nil {25 return err26 }27 configMapNames := []string{newConfigMap.Name}28 existingConfigMap, _ := configMapLister.ConfigMaps(newConfigMap.Namespace).Get(newConfigMap.Name)29 if existingConfigMap != nil {30 if !reflect.DeepEqual(existingConfigMap.Data, newConfigMap.Data) {31 // Don't mess with owner references or labels - VMO makes itself the "owner" of this object32 newConfigMap.OwnerReferences = existingConfigMap.OwnerReferences33 newConfigMap.Labels = existingConfigMap.Labels34 zap.S().Infof("Updating ConfigMap %s", newConfigMap.Name)35 _, err = kubeClientSet.CoreV1().ConfigMaps(newConfigMap.Namespace).Update(context.TODO(), newConfigMap, metav1.UpdateOptions{})36 if err != nil {37 return err38 }39 }40 }41 // Delete ConfigMaps that shouldn't exist42 selector := labels.SelectorFromSet(map[string]string{constants.VerrazzanoBinding: binding.Name})43 existingConfigMapsList, err := configMapLister.ConfigMaps(constants.VerrazzanoNamespace).List(selector)44 if err != nil {45 return err46 }47 for _, existingConfigMap := range existingConfigMapsList {48 if !util.Contains(configMapNames, existingConfigMap.Name) {49 zap.S().Infof("Deleting ConfigMap %s", existingConfigMap.Name)50 err := kubeClientSet.CoreV1().ConfigMaps(existingConfigMap.Namespace).Delete(context.TODO(), existingConfigMap.Name, metav1.DeleteOptions{})51 if err != nil {52 return err53 }54 }55 }56 return nil57}58// DeleteConfigMaps deletes config maps for a given binding in the management cluster.59func DeleteConfigMaps(binding *types.SyntheticBinding, kubeClientSet kubernetes.Interface, configMapLister corev1listers.ConfigMapLister) error {60 zap.S().Debugf("Deleting Local (Management SynModel) configMaps for VerrazzanoBinding %s", binding.Name)61 selector := labels.SelectorFromSet(map[string]string{constants.VerrazzanoBinding: binding.Name})62 existingConfigMapsList, err := configMapLister.ConfigMaps("").List(selector)63 if err != nil {64 return err65 }66 for _, existingConfigMap := range existingConfigMapsList {67 zap.S().Infof("Deleting configMap %s", existingConfigMap.Name)68 err := kubeClientSet.CoreV1().ConfigMaps(existingConfigMap.Namespace).Delete(context.TODO(), existingConfigMap.Name, metav1.DeleteOptions{})69 if err != nil {70 return err71 }72 }73 return nil74}75// Constructs the necessary ConfigMaps for the given VerrazzanoBinding76func newConfigMap(binding *types.SyntheticBinding) (*corev1.ConfigMap, error) {77 bindingLabels := util.GetLocalBindingLabels(binding)78 configMap := &corev1.ConfigMap{79 ObjectMeta: metav1.ObjectMeta{80 Name: util.GetVmiNameForBinding(binding.Name) + "-dashboards",81 Namespace: constants.VerrazzanoNamespace,82 Labels: bindingLabels,83 },84 Data: map[string]string{},85 }86 // check binding name and if system vmi, use system vmi dashboards87 var dashboards []string88 if util.SharedVMIDefault() {89 // Include the default dashboards with system vmi for dev profile90 if binding.Name == constants.VmiSystemBindingName {91 alldashboards := append(constants.SystemDashboards, constants.DefaultDashboards...)92 dashboards = util.RemoveDuplicateValues(alldashboards)93 }94 } else {95 switch binding.Name {96 case constants.VmiSystemBindingName:97 // Add an entry in the ConfigMap for each of the system dashboards98 dashboards = constants.SystemDashboards99 default:100 // Add an entry in the ConfigMap for each of the default dashboards101 dashboards = constants.DefaultDashboards102 }103 }104 for _, dashboard := range dashboards {105 content, err := assets.Asset(dashboard)106 if err != nil {107 return nil, err108 }109 configMap.Data[strings.Replace(dashboard, "/", "-", -1)] = string(content)110 }111 return configMap, nil112}...
removeDuplicateValues
Using AI Code Generation
1import (2func main() {3 var a []int = []int{1, 1, 2, 2, 3, 3, 4, 4, 5, 5}4 fmt.Println("Before removing duplicate values : ", a)5 a = util.RemoveDuplicateValues(a)6 fmt.Println("After removing duplicate values : ", a)7}8func RemoveDuplicateValues(a []int) []int {9 for i := 0; i < len(a); i++ {10 if !Contains(b, a[i]) {11 b = append(b, a[i])12 }13 }14}15func Contains(a []int, e int) bool {16 for i := 0; i < len(a); i++ {17 if a[i] == e {18 }19 }20}
removeDuplicateValues
Using AI Code Generation
1import (2func main() {3 values := []int{1, 2, 2, 3, 4, 5, 5, 6, 7, 8, 9, 10}4 values = util.RemoveDuplicateValues(values)5 fmt.Println(values)6}
removeDuplicateValues
Using AI Code Generation
1import "util"2func main() {3 var arr = []int{1,2,2,3,4,5,5,6,7,8,9,9}4 var result = util.RemoveDuplicateValues(arr)5 fmt.Println(result)6}7func RemoveDuplicateValues(arr []int) []int {8 for _, v := range arr {9 if !contains(result, v) {10 result = append(result, v)11 }12 }13}14func contains(arr []int, val int) bool {15 for _, v := range arr {16 if v == val {17 }18 }19}
removeDuplicateValues
Using AI Code Generation
1import (2func main() {3 var arr []int = []int{1, 2, 3, 4, 4, 5, 5, 5, 6, 7, 7, 8, 9, 9, 9, 9, 9, 10, 11, 11, 11, 11, 11, 11}4 var result []int = util.RemoveDuplicateValues(arr)5 fmt.Println(result)6}7import (8func main() {9 var arr []int = []int{1, 2, 3, 4, 4, 5, 5, 5, 6, 7, 7, 8, 9, 9, 9, 9, 9, 10, 11, 11, 11, 11, 11, 11}10 var result []int = util.RemoveDuplicateValues(arr)11 fmt.Println(result)12}13The go get command checks the import statements of the Go source
removeDuplicateValues
Using AI Code Generation
1import(2func main(){3 fmt.Println("Enter string")4 fmt.Scan(&str)5 fmt.Println("String after removing duplicate characters")6 fmt.Println(util.RemoveDuplicateValues(str))7}8import (9func RemoveDuplicateValues(str string) string{10 for _, char := range str {11 if !containsRune(result, char) {12 result += string(char)13 }14 }15}16func containsRune(s string, r rune) bool {17 for _, c := range s {18 if c == r {19 }20 }21}
removeDuplicateValues
Using AI Code Generation
1import (2func main() {3 fmt.Println("Enter number of elements")4 fmt.Scanln(&noOfElements)5 for i := 0; i < noOfElements; i++ {6 fmt.Scanln(&element)7 input = append(input, element)8 }9 fmt.Println("Input = ", input)10 output := util.RemoveDuplicateValues(input)11 fmt.Println("Output = ", output)12}
removeDuplicateValues
Using AI Code Generation
1import (2func main() {3 var arr = []int{1, 2, 3, 3, 3, 3, 4, 5}4 var arr1 = []int{1, 2, 3, 4, 5}5 fmt.Println("Array with duplicates:", arr)6 arr = util.RemoveDuplicateValues(arr)7 fmt.Println("Array without duplicates:", arr)8 fmt.Println("Array with duplicates:", arr1)9 arr1 = util.RemoveDuplicateValues(arr1)10 fmt.Println("Array without duplicates:", arr1)11}
removeDuplicateValues
Using AI Code Generation
1import (2func main() {3 var arr = []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9}4 fmt.Println("Array elements before removing duplicates: ", arr)5 arr = util.RemoveDuplicateValues(arr)6 fmt.Println("Array elements after removing duplicates: ", arr)7}8func RemoveDuplicateValues(arr []int) []int {9 var tempArr = make([]int, 0)10 for _, value := range arr {11 for _, tempValue := range tempArr {12 if value == tempValue {13 }14 }15 if !isDuplicate {16 tempArr = append(tempArr, value)17 }18 }19}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!