agones.dev/agones@v1.54.0/sdks/go/beta.go (about)

     1  // Copyright 2020 Google LLC All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package sdk
    16  
    17  import (
    18  	"context"
    19  
    20  	"github.com/pkg/errors"
    21  	"google.golang.org/grpc"
    22  	"google.golang.org/protobuf/types/known/fieldmaskpb"
    23  	"google.golang.org/protobuf/types/known/wrapperspb"
    24  
    25  	"agones.dev/agones/pkg/sdk/beta"
    26  )
    27  
    28  // Beta is the struct for Beta SDK functionality.
    29  type Beta struct {
    30  	client beta.SDKClient
    31  }
    32  
    33  // newBeta creates a new Beta SDK with the passed in connection.
    34  func newBeta(conn *grpc.ClientConn) *Beta {
    35  	return &Beta{
    36  		client: beta.NewSDKClient(conn),
    37  	}
    38  }
    39  
    40  // GetCounterCount returns the Count for a Counter, given the Counter's key (name).
    41  // Will error if the key was not predefined in the GameServer resource on creation.
    42  func (b *Beta) GetCounterCount(key string) (int64, error) {
    43  	counter, err := b.client.GetCounter(context.Background(), &beta.GetCounterRequest{Name: key})
    44  	if err != nil {
    45  		return -1, errors.Wrapf(err, "could not get Counter %s count", key)
    46  	}
    47  	return counter.Count, nil
    48  }
    49  
    50  // IncrementCounter increases a counter by the given nonnegative integer amount.
    51  // Will execute the increment operation against the current CRD value. Will max at max(int64).
    52  // Will error if the key was not predefined in the GameServer resource on creation.
    53  // Returns error if the count is at the current capacity (to the latest knowledge of the SDK),
    54  // and no increment will occur.
    55  //
    56  // Note: A potential race condition here is that if count values are set from both the SDK and
    57  // through the K8s API (Allocation or otherwise), since the SDK append operation back to the CRD
    58  // value is batched asynchronous any value incremented past the capacity will be silently truncated.
    59  func (b *Beta) IncrementCounter(key string, amount int64) error {
    60  	if amount < 0 {
    61  		return errors.Errorf("amount must be a positive int64, found %d", amount)
    62  	}
    63  	_, err := b.client.UpdateCounter(context.Background(), &beta.UpdateCounterRequest{
    64  		CounterUpdateRequest: &beta.CounterUpdateRequest{
    65  			Name:      key,
    66  			CountDiff: amount,
    67  		}})
    68  	if err != nil {
    69  		return errors.Wrapf(err, "could not increment Counter %s by amount %d", key, amount)
    70  	}
    71  	return nil
    72  }
    73  
    74  // DecrementCounter decreases the current count by the given nonnegative integer amount.
    75  // The Counter Will not go below 0. Will execute the decrement operation against the current CRD value.
    76  // Will error if the count is at 0 (to the latest knowledge of the SDK), and no decrement will occur.
    77  func (b *Beta) DecrementCounter(key string, amount int64) error {
    78  	if amount < 0 {
    79  		return errors.Errorf("amount must be a positive int64, found %d", amount)
    80  	}
    81  	_, err := b.client.UpdateCounter(context.Background(), &beta.UpdateCounterRequest{
    82  		CounterUpdateRequest: &beta.CounterUpdateRequest{
    83  			Name:      key,
    84  			CountDiff: amount * -1,
    85  		}})
    86  	if err != nil {
    87  		return errors.Wrapf(err, "could not decrement Counter %s by amount %d", key, amount)
    88  	}
    89  	return nil
    90  }
    91  
    92  // SetCounterCount sets a count to the given value. Use with care, as this will overwrite any previous
    93  // invocations’ value. Cannot be greater than Capacity.
    94  func (b *Beta) SetCounterCount(key string, amount int64) error {
    95  	_, err := b.client.UpdateCounter(context.Background(), &beta.UpdateCounterRequest{
    96  		CounterUpdateRequest: &beta.CounterUpdateRequest{
    97  			Name:  key,
    98  			Count: wrapperspb.Int64(amount),
    99  		}})
   100  	if err != nil {
   101  		return errors.Wrapf(err, "could not set Counter %s count to amount %d", key, amount)
   102  	}
   103  	return nil
   104  }
   105  
   106  // GetCounterCapacity returns the Capacity for a Counter, given the Counter's key (name).
   107  // Will error if the key was not predefined in the GameServer resource on creation.
   108  func (b *Beta) GetCounterCapacity(key string) (int64, error) {
   109  	counter, err := b.client.GetCounter(context.Background(), &beta.GetCounterRequest{Name: key})
   110  	if err != nil {
   111  		return -1, errors.Wrapf(err, "could not get Counter %s capacity", key)
   112  	}
   113  	return counter.Capacity, nil
   114  }
   115  
   116  // SetCounterCapacity sets the capacity for the given Counter. A capacity of 0 is no capacity.
   117  func (b *Beta) SetCounterCapacity(key string, amount int64) error {
   118  	_, err := b.client.UpdateCounter(context.Background(), &beta.UpdateCounterRequest{
   119  		CounterUpdateRequest: &beta.CounterUpdateRequest{
   120  			Name:     key,
   121  			Capacity: wrapperspb.Int64(amount),
   122  		}})
   123  	if err != nil {
   124  		return errors.Wrapf(err, "could not set Counter %s capacity to amount %d", key, amount)
   125  	}
   126  	return nil
   127  }
   128  
   129  // GetListCapacity returns the Capacity for a List, given the List's key (name).
   130  // Will error if the key was not predefined in the GameServer resource on creation.
   131  func (b *Beta) GetListCapacity(key string) (int64, error) {
   132  	list, err := b.client.GetList(context.Background(), &beta.GetListRequest{Name: key})
   133  	if err != nil {
   134  		return -1, errors.Wrapf(err, "could not get List %s", key)
   135  	}
   136  	return list.Capacity, nil
   137  }
   138  
   139  // SetListCapacity sets the capacity for a given list. Capacity must be between 0 and 1000.
   140  // Will error if the key was not predefined in the GameServer resource on creation.
   141  func (b *Beta) SetListCapacity(key string, amount int64) error {
   142  	_, err := b.client.UpdateList(context.Background(), &beta.UpdateListRequest{
   143  		List: &beta.List{
   144  			Name:     key,
   145  			Capacity: amount,
   146  		},
   147  		UpdateMask: &fieldmaskpb.FieldMask{Paths: []string{"capacity"}},
   148  	})
   149  	if err != nil {
   150  		return errors.Wrapf(err, "could not set List %s capacity to amount %d", key, amount)
   151  	}
   152  	return nil
   153  }
   154  
   155  // ListContains returns if a string exists in a List's values list, given the List's key (name)
   156  // and the string value. Search is case-sensitive.
   157  // Will error if the key was not predefined in the GameServer resource on creation.
   158  func (b *Beta) ListContains(key, value string) (bool, error) {
   159  	list, err := b.client.GetList(context.Background(), &beta.GetListRequest{Name: key})
   160  	if err != nil {
   161  		return false, errors.Wrapf(err, "could not get List %s", key)
   162  	}
   163  	for _, val := range list.Values {
   164  		if val == value {
   165  			return true, nil
   166  		}
   167  	}
   168  	return false, nil
   169  }
   170  
   171  // GetListLength returns the length of the Values list for a List, given the List's key (name).
   172  // Will error if the key was not predefined in the GameServer resource on creation.
   173  func (b *Beta) GetListLength(key string) (int, error) {
   174  	list, err := b.client.GetList(context.Background(), &beta.GetListRequest{Name: key})
   175  	if err != nil {
   176  		return -1, errors.Wrapf(err, "could not get List %s", key)
   177  	}
   178  	return len(list.Values), nil
   179  }
   180  
   181  // GetListValues returns the Values for a List, given the List's key (name).
   182  // Will error if the key was not predefined in the GameServer resource on creation.
   183  func (b *Beta) GetListValues(key string) ([]string, error) {
   184  	list, err := b.client.GetList(context.Background(), &beta.GetListRequest{Name: key})
   185  	if err != nil {
   186  		return nil, errors.Wrapf(err, "could not get List %s", key)
   187  	}
   188  	return list.Values, nil
   189  }
   190  
   191  // AppendListValue appends a string to a List's values list, given the List's key (name)
   192  // and the string value. Will error if the string already exists in the list.
   193  // Will error if the key was not predefined in the GameServer resource on creation.
   194  func (b *Beta) AppendListValue(key, value string) error {
   195  	_, err := b.client.AddListValue(context.Background(), &beta.AddListValueRequest{Name: key, Value: value})
   196  	if err != nil {
   197  		return errors.Wrapf(err, "could not get List %s", key)
   198  	}
   199  	return nil
   200  }
   201  
   202  // DeleteListValue removes a string from a List's values list, given the List's key (name)
   203  // and the string value. Will error if the string does not exist in the list.
   204  // Will error if the key was not predefined in the GameServer resource on creation.
   205  func (b *Beta) DeleteListValue(key, value string) error {
   206  	_, err := b.client.RemoveListValue(context.Background(), &beta.RemoveListValueRequest{Name: key, Value: value})
   207  	if err != nil {
   208  		return errors.Wrapf(err, "could not get List %s", key)
   209  	}
   210  	return nil
   211  }