github.com/sacloud/iaas-api-go@v1.12.0/helper/api/caller.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 api
    16  
    17  import (
    18  	"strings"
    19  	"time"
    20  
    21  	client "github.com/sacloud/api-client-go"
    22  	"github.com/sacloud/iaas-api-go"
    23  	"github.com/sacloud/iaas-api-go/defaults"
    24  	"github.com/sacloud/iaas-api-go/fake"
    25  	"github.com/sacloud/iaas-api-go/trace"
    26  )
    27  
    28  func NewCaller() (iaas.APICaller, error) {
    29  	clientOpts, err := client.DefaultOption()
    30  	if err != nil {
    31  		return nil, err
    32  	}
    33  	return NewCallerWithOptions(&CallerOptions{Options: clientOpts}), nil
    34  }
    35  
    36  // NewCallerWithOptions 指定のオプションでiaas.APICallerを構築して返す
    37  func NewCallerWithOptions(opts *CallerOptions) iaas.APICaller {
    38  	return newCaller(opts)
    39  }
    40  
    41  func newCaller(opts *CallerOptions) iaas.APICaller {
    42  	if opts.UserAgent == "" {
    43  		opts.UserAgent = iaas.DefaultUserAgent
    44  	}
    45  
    46  	caller := iaas.NewClientWithOptions(opts.Options)
    47  
    48  	defaults.DefaultStatePollingTimeout = 72 * time.Hour
    49  
    50  	if opts.TraceAPI {
    51  		// note: exact once
    52  		trace.AddClientFactoryHooks()
    53  	}
    54  
    55  	if opts.FakeMode {
    56  		if opts.FakeStorePath != "" {
    57  			fake.DataStore = fake.NewJSONFileStore(opts.FakeStorePath)
    58  		}
    59  		// note: exact once
    60  		fake.SwitchFactoryFuncToFake()
    61  
    62  		SetupFakeDefaults()
    63  	}
    64  
    65  	if opts.DefaultZone != "" {
    66  		iaas.APIDefaultZone = opts.DefaultZone
    67  	}
    68  
    69  	if len(opts.Zones) > 0 {
    70  		iaas.SakuraCloudZones = opts.Zones
    71  	}
    72  
    73  	if opts.APIRootURL != "" {
    74  		if strings.HasSuffix(opts.APIRootURL, "/") {
    75  			opts.APIRootURL = strings.TrimRight(opts.APIRootURL, "/")
    76  		}
    77  		iaas.SakuraCloudAPIRoot = opts.APIRootURL
    78  	}
    79  
    80  	return caller
    81  }
    82  
    83  func SetupFakeDefaults() {
    84  	defaultInterval := 10 * time.Millisecond
    85  
    86  	// update default polling intervals: libsacloud/sacloud
    87  	defaults.DefaultStatePollingInterval = defaultInterval
    88  	defaults.DefaultDBStatusPollingInterval = defaultInterval
    89  
    90  	// update default polling intervals: libsacloud/helper/setup
    91  	// update default polling intervals: libsacloud/helper/builder
    92  	defaults.DefaultNICUpdateWaitDuration = defaultInterval
    93  	// update default timeouts and span: libsacloud/helper/power
    94  	defaults.DefaultPowerHelperBootRetrySpan = defaultInterval
    95  	defaults.DefaultPowerHelperShutdownRetrySpan = defaultInterval
    96  	defaults.DefaultPowerHelperInitialRequestRetrySpan = defaultInterval
    97  	defaults.DefaultPowerHelperInitialRequestTimeout = defaultInterval * 100
    98  
    99  	fake.PowerOnDuration = time.Millisecond
   100  	fake.PowerOffDuration = time.Millisecond
   101  	fake.DiskCopyDuration = time.Millisecond
   102  }