github.com/sacloud/libsacloud/v2@v2.32.3/helper/builder/sim/builder.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 sim
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  
    21  	"github.com/sacloud/libsacloud/v2/helper/query"
    22  	"github.com/sacloud/libsacloud/v2/sacloud"
    23  	"github.com/sacloud/libsacloud/v2/sacloud/types"
    24  )
    25  
    26  // Builder SIMのセットアップを行う
    27  type Builder struct {
    28  	Name        string
    29  	Description string
    30  	Tags        types.Tags
    31  	IconID      types.ID
    32  	ICCID       string
    33  	PassCode    string
    34  
    35  	Activate bool
    36  	IMEI     string
    37  	Carrier  []*sacloud.SIMNetworkOperatorConfig
    38  
    39  	Client *APIClient
    40  }
    41  
    42  // Validate 値の検証
    43  func (b *Builder) Validate(ctx context.Context) error {
    44  	if b.ICCID == "" {
    45  		return fmt.Errorf("iccid is required")
    46  	}
    47  	if len(b.Carrier) == 0 {
    48  		return fmt.Errorf("carrier is required")
    49  	}
    50  	return nil
    51  }
    52  
    53  // Build SIMの作成
    54  func (b *Builder) Build(ctx context.Context) (*sacloud.SIM, error) {
    55  	if err := b.Validate(ctx); err != nil {
    56  		return nil, err
    57  	}
    58  
    59  	sim, err := b.Client.SIM.Create(ctx, &sacloud.SIMCreateRequest{
    60  		Name:        b.Name,
    61  		Description: b.Description,
    62  		Tags:        b.Tags,
    63  		IconID:      b.IconID,
    64  		ICCID:       b.ICCID,
    65  		PassCode:    b.PassCode,
    66  	})
    67  	if err != nil {
    68  		return nil, err
    69  	}
    70  
    71  	if err := b.Client.SIM.SetNetworkOperator(ctx, sim.ID, b.Carrier); err != nil {
    72  		return sim, err
    73  	}
    74  
    75  	if b.Activate {
    76  		if err := b.Client.SIM.Activate(ctx, sim.ID); err != nil {
    77  			return sim, err
    78  		}
    79  	}
    80  
    81  	if b.IMEI != "" {
    82  		if err := b.Client.SIM.IMEILock(ctx, sim.ID, &sacloud.SIMIMEILockRequest{IMEI: b.IMEI}); err != nil {
    83  			return sim, err
    84  		}
    85  	}
    86  
    87  	// reload
    88  	refreshed, err := query.FindSIMByID(ctx, b.Client.SIM, sim.ID)
    89  	if err != nil {
    90  		return sim, err
    91  	}
    92  	return refreshed, nil
    93  }
    94  
    95  // Update SIMの更新
    96  func (b *Builder) Update(ctx context.Context, id types.ID) (*sacloud.SIM, error) {
    97  	if err := b.Validate(ctx); err != nil {
    98  		return nil, err
    99  	}
   100  
   101  	sim, err := query.FindSIMByID(ctx, b.Client.SIM, id)
   102  	if err != nil {
   103  		return nil, err
   104  	}
   105  
   106  	_, err = b.Client.SIM.Update(ctx, id, &sacloud.SIMUpdateRequest{
   107  		Name:        b.Name,
   108  		Description: b.Description,
   109  		Tags:        b.Tags,
   110  		IconID:      b.IconID,
   111  	})
   112  	if err != nil {
   113  		return nil, err
   114  	}
   115  
   116  	if err := b.Client.SIM.SetNetworkOperator(ctx, sim.ID, b.Carrier); err != nil {
   117  		return nil, err
   118  	}
   119  
   120  	if !b.Activate && sim.Info.Activated {
   121  		if err := b.Client.SIM.Deactivate(ctx, sim.ID); err != nil {
   122  			return nil, err
   123  		}
   124  	}
   125  	if b.Activate && !sim.Info.Activated {
   126  		if err := b.Client.SIM.Activate(ctx, sim.ID); err != nil {
   127  			return nil, err
   128  		}
   129  	}
   130  
   131  	// Unlock -> ロックされている、かつIMEIが空か前と変わっている場合
   132  	if sim.Info.IMEILock && (b.IMEI == "" || b.IMEI != sim.Info.IMEI) {
   133  		if err := b.Client.SIM.IMEIUnlock(ctx, sim.ID); err != nil {
   134  			return nil, err
   135  		}
   136  	}
   137  	// Lock -> IMEIが変わっている場合は上でアンロックされた状態になっているはず
   138  	if b.IMEI != "" && b.IMEI != sim.Info.IMEI {
   139  		if err := b.Client.SIM.IMEILock(ctx, sim.ID, &sacloud.SIMIMEILockRequest{IMEI: b.IMEI}); err != nil {
   140  			return nil, err
   141  		}
   142  	}
   143  
   144  	// reload
   145  	sim, err = query.FindSIMByID(ctx, b.Client.SIM, id)
   146  	if err != nil {
   147  		return nil, err
   148  	}
   149  	return sim, nil
   150  }