github.com/sacloud/iaas-api-go@v1.12.0/helper/power/handlers.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 power
    16  
    17  import (
    18  	"context"
    19  	"strings"
    20  
    21  	"github.com/sacloud/iaas-api-go"
    22  	"github.com/sacloud/iaas-api-go/types"
    23  )
    24  
    25  // ServerAPI APIクライアント
    26  type ServerAPI interface {
    27  	Read(ctx context.Context, zone string, id types.ID) (*iaas.Server, error)
    28  	Boot(ctx context.Context, zone string, id types.ID) error
    29  	BootWithVariables(ctx context.Context, zone string, id types.ID, param *iaas.ServerBootVariables) error
    30  	Shutdown(ctx context.Context, zone string, id types.ID, shutdownOption *iaas.ShutdownOption) error
    31  }
    32  
    33  type serverHandler struct {
    34  	ctx       context.Context
    35  	client    ServerAPI
    36  	zone      string
    37  	id        types.ID
    38  	variables []string
    39  }
    40  
    41  func (h *serverHandler) boot() error {
    42  	if len(h.variables) > 0 && strings.Join(h.variables, "") != "" {
    43  		userData := strings.Join(h.variables, "\n")
    44  		return h.client.BootWithVariables(h.ctx, h.zone, h.id, &iaas.ServerBootVariables{UserData: userData})
    45  	}
    46  	return h.client.Boot(h.ctx, h.zone, h.id)
    47  }
    48  
    49  func (h *serverHandler) shutdown(force bool) error {
    50  	return h.client.Shutdown(h.ctx, h.zone, h.id, &iaas.ShutdownOption{Force: force})
    51  }
    52  
    53  func (h *serverHandler) read() (interface{}, error) {
    54  	return h.client.Read(h.ctx, h.zone, h.id)
    55  }
    56  
    57  // LoadBalancerAPI APIクライアント
    58  type LoadBalancerAPI interface {
    59  	Read(ctx context.Context, zone string, id types.ID) (*iaas.LoadBalancer, error)
    60  	Boot(ctx context.Context, zone string, id types.ID) error
    61  	Shutdown(ctx context.Context, zone string, id types.ID, shutdownOption *iaas.ShutdownOption) error
    62  }
    63  
    64  type loadBalancerHandler struct {
    65  	ctx    context.Context
    66  	client LoadBalancerAPI
    67  	zone   string
    68  	id     types.ID
    69  }
    70  
    71  func (h *loadBalancerHandler) boot() error {
    72  	return h.client.Boot(h.ctx, h.zone, h.id)
    73  }
    74  
    75  func (h *loadBalancerHandler) shutdown(force bool) error {
    76  	return h.client.Shutdown(h.ctx, h.zone, h.id, &iaas.ShutdownOption{Force: force})
    77  }
    78  
    79  func (h *loadBalancerHandler) read() (interface{}, error) {
    80  	return h.client.Read(h.ctx, h.zone, h.id)
    81  }
    82  
    83  // DatabaseAPI APIクライアント
    84  type DatabaseAPI interface {
    85  	Read(ctx context.Context, zone string, id types.ID) (*iaas.Database, error)
    86  	Boot(ctx context.Context, zone string, id types.ID) error
    87  	Shutdown(ctx context.Context, zone string, id types.ID, shutdownOption *iaas.ShutdownOption) error
    88  }
    89  
    90  type databaseHandler struct {
    91  	ctx    context.Context
    92  	client DatabaseAPI
    93  	zone   string
    94  	id     types.ID
    95  }
    96  
    97  func (h *databaseHandler) boot() error {
    98  	return h.client.Boot(h.ctx, h.zone, h.id)
    99  }
   100  
   101  func (h *databaseHandler) shutdown(force bool) error {
   102  	return h.client.Shutdown(h.ctx, h.zone, h.id, &iaas.ShutdownOption{Force: force})
   103  }
   104  
   105  func (h *databaseHandler) read() (interface{}, error) {
   106  	return h.client.Read(h.ctx, h.zone, h.id)
   107  }
   108  
   109  // VPCRouterAPI APIクライアント
   110  type VPCRouterAPI interface {
   111  	Read(ctx context.Context, zone string, id types.ID) (*iaas.VPCRouter, error)
   112  	Boot(ctx context.Context, zone string, id types.ID) error
   113  	Shutdown(ctx context.Context, zone string, id types.ID, shutdownOption *iaas.ShutdownOption) error
   114  }
   115  
   116  type vpcRouterHandler struct {
   117  	ctx    context.Context
   118  	client VPCRouterAPI
   119  	zone   string
   120  	id     types.ID
   121  }
   122  
   123  func (h *vpcRouterHandler) boot() error {
   124  	return h.client.Boot(h.ctx, h.zone, h.id)
   125  }
   126  
   127  func (h *vpcRouterHandler) shutdown(force bool) error {
   128  	return h.client.Shutdown(h.ctx, h.zone, h.id, &iaas.ShutdownOption{Force: force})
   129  }
   130  
   131  func (h *vpcRouterHandler) read() (interface{}, error) {
   132  	return h.client.Read(h.ctx, h.zone, h.id)
   133  }
   134  
   135  // NFSAPI APIクライアント
   136  type NFSAPI interface {
   137  	Read(ctx context.Context, zone string, id types.ID) (*iaas.NFS, error)
   138  	Boot(ctx context.Context, zone string, id types.ID) error
   139  	Shutdown(ctx context.Context, zone string, id types.ID, shutdownOption *iaas.ShutdownOption) error
   140  }
   141  
   142  type nfsHandler struct {
   143  	ctx    context.Context
   144  	client NFSAPI
   145  	zone   string
   146  	id     types.ID
   147  }
   148  
   149  func (h *nfsHandler) boot() error {
   150  	return h.client.Boot(h.ctx, h.zone, h.id)
   151  }
   152  
   153  func (h *nfsHandler) shutdown(force bool) error {
   154  	return h.client.Shutdown(h.ctx, h.zone, h.id, &iaas.ShutdownOption{Force: force})
   155  }
   156  
   157  func (h *nfsHandler) read() (interface{}, error) {
   158  	return h.client.Read(h.ctx, h.zone, h.id)
   159  }
   160  
   161  // MobileGatewayAPI APIクライアント
   162  type MobileGatewayAPI interface {
   163  	Read(ctx context.Context, zone string, id types.ID) (*iaas.MobileGateway, error)
   164  	Boot(ctx context.Context, zone string, id types.ID) error
   165  	Shutdown(ctx context.Context, zone string, id types.ID, shutdownOption *iaas.ShutdownOption) error
   166  }
   167  
   168  type mobileGatewayHandler struct {
   169  	ctx    context.Context
   170  	client MobileGatewayAPI
   171  	zone   string
   172  	id     types.ID
   173  }
   174  
   175  func (h *mobileGatewayHandler) boot() error {
   176  	return h.client.Boot(h.ctx, h.zone, h.id)
   177  }
   178  
   179  func (h *mobileGatewayHandler) shutdown(force bool) error {
   180  	return h.client.Shutdown(h.ctx, h.zone, h.id, &iaas.ShutdownOption{Force: force})
   181  }
   182  
   183  func (h *mobileGatewayHandler) read() (interface{}, error) {
   184  	return h.client.Read(h.ctx, h.zone, h.id)
   185  }