github.com/sacloud/libsacloud/v2@v2.32.3/helper/service/mobilegateway/update_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  	"context"
    19  
    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 UpdateRequest struct {
    27  	Zone string   `validate:"required"`
    28  	ID   types.ID `validate:"required"`
    29  
    30  	Name                            *string                              `request:",omitempty"`
    31  	Description                     *string                              `request:",omitempty" validate:"omitempty,min=0,max=512"`
    32  	Tags                            *types.Tags                          `request:",omitempty"`
    33  	IconID                          *types.ID                            `request:",omitempty"`
    34  	PrivateInterface                *PrivateInterfaceSettingUpdate       `request:",omitempty,recursive"`
    35  	StaticRoutes                    *[]*sacloud.MobileGatewayStaticRoute `request:",omitempty"`
    36  	SIMRoutes                       *[]*SIMRouteSetting                  `request:",omitempty"`
    37  	InternetConnectionEnabled       *bool                                `request:",omitempty"`
    38  	InterDeviceCommunicationEnabled *bool                                `request:",omitempty"`
    39  	DNS                             *DNSSettingUpdate                    `request:",omitempty,recursive"`
    40  	SIMs                            *[]*SIMSetting                       `request:",omitempty"`
    41  	TrafficConfig                   *TrafficConfigUpdate                 `request:",omitempty,recursive"`
    42  
    43  	SettingsHash string
    44  	NoWait       bool
    45  }
    46  
    47  // PrivateInterfaceSetting represents API parameter/response structure
    48  type PrivateInterfaceSettingUpdate struct {
    49  	SwitchID       *types.ID `request:",omitempty"`
    50  	IPAddress      *string   `request:",omitempty" validate:"omitempty,ipv4"`
    51  	NetworkMaskLen *int      `request:",omitempty"`
    52  }
    53  
    54  type DNSSettingUpdate struct {
    55  	DNS1 *string `request:",omitempty" validate:"required_with=DNS2,omitempty,ipv4"`
    56  	DNS2 *string `request:",omitempty" validate:"required_with=DNS1,omitempty,ipv4"`
    57  }
    58  
    59  type TrafficConfigUpdate struct {
    60  	TrafficQuotaInMB       *int    `request:",omitempty"`
    61  	BandWidthLimitInKbps   *int    `request:",omitempty"`
    62  	EmailNotifyEnabled     *bool   `request:",omitempty"`
    63  	SlackNotifyEnabled     *bool   `request:",omitempty"`
    64  	SlackNotifyWebhooksURL *string `request:",omitempty"`
    65  	AutoTrafficShaping     *bool   `request:",omitempty"`
    66  }
    67  
    68  func (req *UpdateRequest) Validate() error {
    69  	return validate.Struct(req)
    70  }
    71  
    72  func (req *UpdateRequest) ApplyRequest(ctx context.Context, caller sacloud.APICaller) (*ApplyRequest, error) {
    73  	mgwOp := sacloud.NewMobileGatewayOp(caller)
    74  	current, err := mgwOp.Read(ctx, req.Zone, req.ID)
    75  	if err != nil {
    76  		return nil, err
    77  	}
    78  
    79  	var privateInterface *PrivateInterfaceSetting
    80  	for _, nic := range current.InterfaceSettings {
    81  		if nic.Index == 1 && len(current.Interfaces) > 1 {
    82  			privateInterface = &PrivateInterfaceSetting{
    83  				SwitchID:       current.Interfaces[nic.Index].SwitchID,
    84  				IPAddress:      nic.IPAddress[0],
    85  				NetworkMaskLen: nic.NetworkMaskLen,
    86  			}
    87  		}
    88  	}
    89  
    90  	simRoutes, err := mgwOp.GetSIMRoutes(ctx, req.Zone, req.ID)
    91  	if err != nil {
    92  		return nil, err
    93  	}
    94  	var simRouteSettings []*SIMRouteSetting
    95  	for _, r := range simRoutes {
    96  		simRouteSettings = append(simRouteSettings, &SIMRouteSetting{
    97  			SIMID:  types.StringID(r.ResourceID),
    98  			Prefix: r.Prefix,
    99  		})
   100  	}
   101  
   102  	currentDNS, err := mgwOp.GetDNS(ctx, req.Zone, req.ID)
   103  	if err != nil {
   104  		return nil, err
   105  	}
   106  	var dns *DNSSetting
   107  	if currentDNS != nil {
   108  		dns = &DNSSetting{
   109  			DNS1: currentDNS.DNS1,
   110  			DNS2: currentDNS.DNS2,
   111  		}
   112  	}
   113  
   114  	sims, err := mgwOp.ListSIM(ctx, req.Zone, req.ID)
   115  	if err != nil {
   116  		return nil, err
   117  	}
   118  	var simSettings []*SIMSetting
   119  	for _, s := range sims {
   120  		simSettings = append(simSettings, &SIMSetting{
   121  			SIMID:     types.StringID(s.ResourceID),
   122  			IPAddress: s.IP,
   123  		})
   124  	}
   125  
   126  	currentTrafficConfig, err := mgwOp.GetTrafficConfig(ctx, req.Zone, req.ID)
   127  	if err != nil {
   128  		return nil, err
   129  	}
   130  	var trafficConfig *TrafficConfig
   131  	if currentTrafficConfig != nil {
   132  		trafficConfig = &TrafficConfig{}
   133  		if err := service.RequestConvertTo(currentTrafficConfig, trafficConfig); err != nil {
   134  			return nil, err
   135  		}
   136  	}
   137  
   138  	applyRequest := &ApplyRequest{
   139  		Name:                            current.Name,
   140  		Description:                     current.Description,
   141  		Tags:                            current.Tags,
   142  		IconID:                          current.IconID,
   143  		PrivateInterface:                privateInterface,
   144  		StaticRoutes:                    current.StaticRoutes,
   145  		SIMRoutes:                       simRouteSettings,
   146  		InternetConnectionEnabled:       current.InternetConnectionEnabled.Bool(),
   147  		InterDeviceCommunicationEnabled: current.InterDeviceCommunicationEnabled.Bool(),
   148  		DNS:                             dns,
   149  		SIMs:                            simSettings,
   150  		TrafficConfig:                   trafficConfig,
   151  		SettingsHash:                    current.SettingsHash,
   152  	}
   153  
   154  	if err := service.RequestConvertTo(req, applyRequest); err != nil {
   155  		return nil, err
   156  	}
   157  	return applyRequest, nil
   158  }