sigs.k8s.io/cluster-api-provider-azure@v1.14.3/util/futures/setter.go (about)

     1  /*
     2  Copyright 2021 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package futures
    18  
    19  import (
    20  	infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
    21  )
    22  
    23  // Setter interface defines methods that an object should implement in order to
    24  // use the futures package for setting futures.
    25  type Setter interface {
    26  	Getter
    27  	SetFutures(infrav1.Futures)
    28  }
    29  
    30  // Set sets the given future.
    31  //
    32  // NOTE: If a future already exists, we update it.
    33  func Set(to Setter, future *infrav1.Future) {
    34  	if to == nil || future == nil {
    35  		return
    36  	}
    37  
    38  	// Check if the new future already exists, and update it if it does.
    39  	futures := to.GetFutures()
    40  	exists := false
    41  	for i, f := range futures {
    42  		if f.Name == future.Name && f.ServiceName == future.ServiceName {
    43  			exists = true
    44  			futures[i] = *future
    45  			break
    46  		}
    47  	}
    48  
    49  	// If the future does not exist, add it.
    50  	if !exists {
    51  		futures = append(futures, *future)
    52  	}
    53  
    54  	to.SetFutures(futures)
    55  }
    56  
    57  // Delete deletes the specified future.
    58  func Delete(to Setter, name, service, futureType string) {
    59  	if to == nil || name == "" || service == "" || futureType == "" {
    60  		return
    61  	}
    62  
    63  	futures := to.GetFutures()
    64  	for i, f := range futures {
    65  		if f.Name == name && f.ServiceName == service && f.Type == futureType {
    66  			futures = append(futures[:i], futures[i+1:]...)
    67  			break
    68  		}
    69  	}
    70  
    71  	to.SetFutures(futures)
    72  }