customize_mobile_gateway.go raw

   1  // Copyright 2022-2025 The sacloud/iaas-api-go Authors
   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 iaas
  16  
  17  import (
  18  	"fmt"
  19  
  20  	"github.com/sacloud/iaas-api-go/types"
  21  )
  22  
  23  type MobileGatewaySIMRoutes []*MobileGatewaySIMRoute
  24  
  25  func (o *MobileGatewaySIMRoutes) FindByID(resourceID types.ID) *MobileGatewaySIMRoute {
  26  	for _, r := range *o {
  27  		if r.ResourceID == resourceID.String() {
  28  			return r
  29  		}
  30  	}
  31  	return nil
  32  }
  33  
  34  func (o *MobileGatewaySIMRoutes) Exists(resourceID types.ID) bool {
  35  	return o.FindByID(resourceID) != nil
  36  }
  37  
  38  func (o *MobileGatewaySIMRoutes) Add(route *MobileGatewaySIMRoute) error {
  39  	if o.Exists(types.StringID(route.ResourceID)) {
  40  		return fmt.Errorf("SIM[%s] already exists in SIMRoutes", route.ResourceID)
  41  	}
  42  	*o = append(*o, route)
  43  	return nil
  44  }
  45  
  46  func (o *MobileGatewaySIMRoutes) Update(route *MobileGatewaySIMRoute) error {
  47  	r := o.FindByID(types.StringID(route.ResourceID))
  48  	if r == nil {
  49  		return fmt.Errorf("SIM[%s] not found in SIMRoutes", route.ResourceID)
  50  	}
  51  	r.Prefix = route.Prefix
  52  	r.ICCID = route.ICCID
  53  	return nil
  54  }
  55  
  56  func (o *MobileGatewaySIMRoutes) Delete(resourceID types.ID) error {
  57  	if !o.Exists(resourceID) {
  58  		return fmt.Errorf("SIM[%s] not found in SIMRoutes", resourceID)
  59  	}
  60  	var rs []*MobileGatewaySIMRoute
  61  	for _, r := range *o {
  62  		if r.ResourceID != resourceID.String() {
  63  			rs = append(rs, r)
  64  		}
  65  	}
  66  	*o = rs
  67  	return nil
  68  }
  69  
  70  func (o *MobileGatewaySIMRoutes) ToRequestParameter() []*MobileGatewaySIMRouteParam {
  71  	var rs []*MobileGatewaySIMRouteParam
  72  	for _, r := range *o {
  73  		rs = append(rs, &MobileGatewaySIMRouteParam{
  74  			ResourceID: r.ResourceID,
  75  			Prefix:     r.Prefix,
  76  		})
  77  	}
  78  	return rs
  79  }
  80  
  81  type MobileGatewaySIMs []*MobileGatewaySIMInfo
  82  
  83  func (o *MobileGatewaySIMs) FindByID(resourceID types.ID) *MobileGatewaySIMInfo {
  84  	for _, s := range *o {
  85  		if s.ResourceID == resourceID.String() {
  86  			return s
  87  		}
  88  	}
  89  	return nil
  90  }
  91  
  92  func (o *MobileGatewaySIMs) Exists(resourceID types.ID) bool {
  93  	return o.FindByID(resourceID) != nil
  94  }
  95