github.com/sacloud/iaas-api-go@v1.12.0/helper/cleanup/mobile_gateway.go (about)

     1  // Copyright 2022-2023 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 cleanup
    16  
    17  import (
    18  	"context"
    19  
    20  	"github.com/sacloud/iaas-api-go"
    21  	"github.com/sacloud/iaas-api-go/helper/power"
    22  	"github.com/sacloud/iaas-api-go/types"
    23  )
    24  
    25  // DeleteMobileGateway 削除
    26  func DeleteMobileGateway(ctx context.Context, mgwAPI iaas.MobileGatewayAPI, simAPI iaas.SIMAPI, zone string, id types.ID) error {
    27  	// check MobileGateway is exists
    28  	mgw, err := mgwAPI.Read(ctx, zone, id)
    29  	if err != nil {
    30  		return err
    31  	}
    32  
    33  	if mgw.InstanceStatus.IsUp() {
    34  		if err := power.ShutdownMobileGateway(ctx, mgwAPI, zone, id, true); err != nil {
    35  			return err
    36  		}
    37  	}
    38  
    39  	// sim route
    40  	simRoutes, err := mgwAPI.GetSIMRoutes(ctx, zone, id)
    41  	if err != nil {
    42  		return err
    43  	}
    44  	if len(simRoutes) > 0 {
    45  		if err := mgwAPI.SetSIMRoutes(ctx, zone, id, []*iaas.MobileGatewaySIMRouteParam{}); err != nil {
    46  			return err
    47  		}
    48  	}
    49  
    50  	// sim
    51  	sims, err := mgwAPI.ListSIM(ctx, zone, id)
    52  	if err != nil {
    53  		return err
    54  	}
    55  	for _, sim := range sims {
    56  		if err := simAPI.ClearIP(ctx, types.StringID(sim.ResourceID)); err != nil {
    57  			return err
    58  		}
    59  		if err := mgwAPI.DeleteSIM(ctx, zone, id, types.StringID(sim.ResourceID)); err != nil {
    60  			return err
    61  		}
    62  	}
    63  
    64  	return mgwAPI.Delete(ctx, zone, id)
    65  }