github.com/sacloud/iaas-api-go@v1.12.0/internal/define/switch.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 define
    16  
    17  import (
    18  	"net/http"
    19  
    20  	"github.com/sacloud/iaas-api-go/internal/define/names"
    21  	"github.com/sacloud/iaas-api-go/internal/define/ops"
    22  	"github.com/sacloud/iaas-api-go/internal/dsl"
    23  	"github.com/sacloud/iaas-api-go/internal/dsl/meta"
    24  	"github.com/sacloud/iaas-api-go/naked"
    25  )
    26  
    27  const (
    28  	switchAPIName     = "Switch"
    29  	switchAPIPathName = "switch"
    30  )
    31  
    32  var switchAPI = &dsl.Resource{
    33  	Name:       switchAPIName,
    34  	PathName:   switchAPIPathName,
    35  	PathSuffix: dsl.CloudAPISuffix,
    36  	Operations: dsl.Operations{
    37  		// find
    38  		ops.Find(switchAPIName, switchNakedType, findParameter, switchView),
    39  
    40  		// create
    41  		ops.Create(switchAPIName, switchNakedType, switchCreateParam, switchView),
    42  
    43  		// read
    44  		ops.Read(switchAPIName, switchNakedType, switchView),
    45  
    46  		// update
    47  		ops.Update(switchAPIName, switchNakedType, switchUpdateParam, switchView),
    48  
    49  		// delete
    50  		ops.Delete(switchAPIName),
    51  
    52  		// connect from bridge
    53  		ops.WithIDAction(switchAPIName, "ConnectToBridge", http.MethodPut, "to/bridge/{{.bridgeID}}",
    54  			&dsl.Argument{
    55  				Name: "bridgeID",
    56  				Type: meta.TypeID,
    57  			},
    58  		),
    59  
    60  		// disconnect from bridge
    61  		ops.WithIDAction(switchAPIName, "DisconnectFromBridge", http.MethodDelete, "to/bridge/"),
    62  
    63  		// find connected servers
    64  		{
    65  			ResourceName:     switchAPIName,
    66  			Name:             "GetServers",
    67  			PathFormat:       dsl.IDAndSuffixPathFormat("server"),
    68  			Method:           http.MethodGet,
    69  			UseWrappedResult: true,
    70  			Arguments: dsl.Arguments{
    71  				dsl.ArgumentID,
    72  			},
    73  			ResponseEnvelope: dsl.ResponseEnvelopePlural(&dsl.EnvelopePayloadDesc{
    74  				Type: serverNakedType,
    75  				Name: names.ResourceFieldName(serverAPIName, dsl.PayloadForms.Plural),
    76  			}),
    77  			Results: dsl.Results{
    78  				{
    79  					SourceField: names.ResourceFieldName(serverAPIName, dsl.PayloadForms.Plural),
    80  					DestField:   names.ResourceFieldName(serverAPIName, dsl.PayloadForms.Plural),
    81  					IsPlural:    true,
    82  					Model:       serverView,
    83  				},
    84  			},
    85  		},
    86  	},
    87  }
    88  
    89  var (
    90  	switchNakedType = meta.Static(naked.Switch{})
    91  
    92  	switchView = &dsl.Model{
    93  		Name:      switchAPIName,
    94  		NakedType: switchNakedType,
    95  		Fields: []*dsl.FieldDesc{
    96  			fields.ID(),
    97  			fields.Name(),
    98  			fields.Description(),
    99  			fields.Tags(),
   100  			fields.IconID(),
   101  			fields.CreatedAt(),
   102  			fields.ModifiedAt(),
   103  			fields.Scope(),
   104  			fields.Def("ServerCount", meta.TypeInt),
   105  			fields.UserSubnetNetworkMaskLen(),
   106  			fields.UserSubnetDefaultRoute(),
   107  			{
   108  				Name: "Subnets",
   109  				Type: models.switchSubnet(),
   110  				Tags: &dsl.FieldTags{
   111  					MapConv: "[]Subnets,omitempty,recursive",
   112  					JSON:    ",omitempty",
   113  				},
   114  			},
   115  			fields.BridgeID(),
   116  			fields.HybridConnectionID(),
   117  		},
   118  	}
   119  
   120  	switchCreateParam = &dsl.Model{
   121  		Name:      names.CreateParameterName(switchAPIName),
   122  		NakedType: switchNakedType,
   123  		Fields: []*dsl.FieldDesc{
   124  			fields.Name(),
   125  			fields.UserSubnetNetworkMaskLen(),
   126  			fields.UserSubnetDefaultRoute(),
   127  			fields.Description(),
   128  			fields.Tags(),
   129  			fields.IconID(),
   130  		},
   131  	}
   132  
   133  	switchUpdateParam = &dsl.Model{
   134  		Name:      names.UpdateParameterName(switchAPIName),
   135  		NakedType: switchNakedType,
   136  		Fields: []*dsl.FieldDesc{
   137  			fields.Name(),
   138  			fields.UserSubnetNetworkMaskLen(),
   139  			fields.UserSubnetDefaultRoute(),
   140  			fields.Description(),
   141  			fields.Tags(),
   142  			fields.IconID(),
   143  		},
   144  	}
   145  )