github.com/sacloud/libsacloud/v2@v2.32.3/helper/service/mobilegateway/apply_request.go (about)

     1  // Copyright 2016-2022 The Libsacloud 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 mobilegateway
    16  
    17  import (
    18  	"github.com/sacloud/libsacloud/v2/helper/builder"
    19  	mobileGatewayBuilder "github.com/sacloud/libsacloud/v2/helper/builder/mobilegateway"
    20  	"github.com/sacloud/libsacloud/v2/helper/service"
    21  	"github.com/sacloud/libsacloud/v2/helper/validate"
    22  	"github.com/sacloud/libsacloud/v2/sacloud"
    23  	"github.com/sacloud/libsacloud/v2/sacloud/types"
    24  )
    25  
    26  type ApplyRequest struct {
    27  	Zone string `request:"-" validate:"required"`
    28  
    29  	ID                              types.ID `request:"-"`
    30  	Name                            string   `validate:"required"`
    31  	Description                     string   `validate:"min=0,max=512"`
    32  	Tags                            types.Tags
    33  	IconID                          types.ID
    34  	PrivateInterface                *PrivateInterfaceSetting `validate:"omitempty"`
    35  	StaticRoutes                    []*sacloud.MobileGatewayStaticRoute
    36  	SIMRoutes                       []*SIMRouteSetting
    37  	InternetConnectionEnabled       bool
    38  	InterDeviceCommunicationEnabled bool
    39  	DNS                             *DNSSetting
    40  	SIMs                            []*SIMSetting
    41  	TrafficConfig                   *TrafficConfig
    42  
    43  	SettingsHash    string
    44  	BootAfterCreate bool
    45  	NoWait          bool
    46  }
    47  
    48  // PrivateInterfaceSetting represents API parameter/response structure
    49  type PrivateInterfaceSetting struct {
    50  	SwitchID       types.ID `request:",omitempty"`
    51  	IPAddress      string   `request:",omitempty" validate:"required,ipv4"`
    52  	NetworkMaskLen int      `request:",omitempty"`
    53  }
    54  
    55  // SIMRouteSetting represents API parameter/response structure
    56  type SIMRouteSetting struct {
    57  	SIMID  types.ID
    58  	Prefix string `validate:"required"`
    59  }
    60  
    61  // SIMSetting represents API parameter/response structure
    62  type SIMSetting struct {
    63  	SIMID     types.ID
    64  	IPAddress string `validate:"required,ipv4"`
    65  }
    66  
    67  type DNSSetting struct {
    68  	DNS1 string `request:",omitempty" validate:"required_with=DNS2,omitempty,ipv4"`
    69  	DNS2 string `request:",omitempty" validate:"required_with=DNS1,omitempty,ipv4"`
    70  }
    71  
    72  type TrafficConfig struct {
    73  	TrafficQuotaInMB       int    `request:",omitempty"`
    74  	BandWidthLimitInKbps   int    `request:",omitempty"`
    75  	EmailNotifyEnabled     bool   `request:",omitempty"`
    76  	SlackNotifyEnabled     bool   `request:",omitempty"`
    77  	SlackNotifyWebhooksURL string `request:",omitempty"`
    78  	AutoTrafficShaping     bool   `request:",omitempty"`
    79  }
    80  
    81  func (req *ApplyRequest) Validate() error {
    82  	return validate.Struct(req)
    83  }
    84  
    85  func (req *ApplyRequest) Builder(caller sacloud.APICaller) (*mobileGatewayBuilder.Builder, error) {
    86  	var privateInterface *mobileGatewayBuilder.PrivateInterfaceSetting
    87  	if req.PrivateInterface != nil {
    88  		privateInterface = &mobileGatewayBuilder.PrivateInterfaceSetting{
    89  			SwitchID:       req.PrivateInterface.SwitchID,
    90  			IPAddress:      req.PrivateInterface.IPAddress,
    91  			NetworkMaskLen: req.PrivateInterface.NetworkMaskLen,
    92  		}
    93  	}
    94  
    95  	var simRoutes []*mobileGatewayBuilder.SIMRouteSetting
    96  	for _, sr := range req.SIMRoutes {
    97  		simRoutes = append(simRoutes, &mobileGatewayBuilder.SIMRouteSetting{
    98  			SIMID:  sr.SIMID,
    99  			Prefix: sr.Prefix,
   100  		})
   101  	}
   102  
   103  	var sims []*mobileGatewayBuilder.SIMSetting
   104  	for _, s := range req.SIMs {
   105  		sims = append(sims, &mobileGatewayBuilder.SIMSetting{
   106  			SIMID:     s.SIMID,
   107  			IPAddress: s.IPAddress,
   108  		})
   109  	}
   110  
   111  	var dns *sacloud.MobileGatewayDNSSetting
   112  	if req.DNS != nil {
   113  		dns = &sacloud.MobileGatewayDNSSetting{
   114  			DNS1: req.DNS.DNS1,
   115  			DNS2: req.DNS.DNS2,
   116  		}
   117  	}
   118  
   119  	var trafficConfig *sacloud.MobileGatewayTrafficControl
   120  	if req.TrafficConfig != nil {
   121  		trafficConfig = &sacloud.MobileGatewayTrafficControl{}
   122  		if err := service.RequestConvertTo(req.TrafficConfig, trafficConfig); err != nil {
   123  			return nil, err
   124  		}
   125  	}
   126  
   127  	return &mobileGatewayBuilder.Builder{
   128  		Name:                            req.Name,
   129  		Description:                     req.Description,
   130  		Tags:                            req.Tags,
   131  		IconID:                          req.IconID,
   132  		PrivateInterface:                privateInterface,
   133  		StaticRoutes:                    req.StaticRoutes,
   134  		SIMRoutes:                       simRoutes,
   135  		InternetConnectionEnabled:       req.InternetConnectionEnabled,
   136  		InterDeviceCommunicationEnabled: req.InterDeviceCommunicationEnabled,
   137  		DNS:                             dns,
   138  		SIMs:                            sims,
   139  		TrafficConfig:                   trafficConfig,
   140  		SettingsHash:                    req.SettingsHash,
   141  		NoWait:                          req.NoWait,
   142  		SetupOptions:                    &builder.RetryableSetupParameter{BootAfterBuild: req.BootAfterCreate},
   143  		Client:                          mobileGatewayBuilder.NewAPIClient(caller),
   144  	}, nil
   145  }