customize_envelope.go raw

   1  // Copyright 2022-2025 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 iaas
  16  
  17  import (
  18  	"encoding/json"
  19  
  20  	"github.com/sacloud/iaas-api-go/naked"
  21  	"github.com/sacloud/iaas-api-go/search"
  22  )
  23  
  24  // Note: sacloud/配下でのUnmarshalJSONの実装
  25  //
  26  // v2/internal/dslではAPIからの戻り値が以下のようにラップされていることを期待している
  27  // {
  28  //    "<API名>": {},
  29  //    "is_ok": true
  30  // }
  31  //
  32  // この形式に沿っていないAPIについてはレスポンスのエンベロープ(struct xxxResponseEnvelope)でUnmarshalJSONを実装する必要がある
  33  
  34  // UnmarshalJSON APIからの戻り値でレスポンスボディ直下にデータを持つことへの対応
  35  func (a *authStatusReadResponseEnvelope) UnmarshalJSON(data []byte) error {
  36  	type alias authStatusReadResponseEnvelope
  37  
  38  	var tmp alias
  39  	if err := json.Unmarshal(data, &tmp); err != nil {
  40  		return err
  41  	}
  42  
  43  	var nakedAuthStatus naked.AuthStatus
  44  	if err := json.Unmarshal(data, &nakedAuthStatus); err != nil {
  45  		return err
  46  	}
  47  	tmp.AuthStatus = &nakedAuthStatus
  48  
  49  	*a = authStatusReadResponseEnvelope(tmp)
  50  	return nil
  51  }
  52  
  53  func (b *billDetailsCSVResponseEnvelope) UnmarshalJSON(data []byte) error {
  54  	type alias billDetailsCSVResponseEnvelope
  55  
  56  	var tmp alias
  57  	if err := json.Unmarshal(data, &tmp); err != nil {
  58  		return err
  59  	}
  60  
  61  	var nakedBillDetailCSV naked.BillDetailCSV
  62  	if err := json.Unmarshal(data, &nakedBillDetailCSV); err != nil {
  63  		return err
  64  	}
  65  	tmp.CSV = &nakedBillDetailCSV
  66  
  67  	*b = billDetailsCSVResponseEnvelope(tmp)
  68  	return nil
  69  }
  70  
  71  // UnmarshalJSON APIからの戻り値でレスポンスボディ直下にデータを持つことへの対応
  72  func (a *certificateAuthorityAddClientResponseEnvelope) UnmarshalJSON(data []byte) error {
  73  	type alias certificateAuthorityAddClientResponseEnvelope
  74  
  75  	// is_okなどの共通的な項目
  76  	var tmp alias
  77  	if err := json.Unmarshal(data, &tmp); err != nil {
  78  		return err
  79  	}
  80  
  81  	// ラッパーが省略されている、レスポンスボディの直下にある項目
  82  	var result naked.CertificateAuthorityAddClientOrServerResult
  83  	if err := json.Unmarshal(data, &result); err != nil {
  84  		return err
  85  	}
  86  	tmp.CertificateAuthority = &result
  87  
  88  	*a = certificateAuthorityAddClientResponseEnvelope(tmp)
  89  	return nil
  90  }
  91  
  92  // UnmarshalJSON APIからの戻り値でレスポンスボディ直下にデータを持つことへの対応
  93  func (a *certificateAuthorityAddServerResponseEnvelope) UnmarshalJSON(data []byte) error {
  94  	type alias certificateAuthorityAddServerResponseEnvelope
  95  
  96  	// is_okなどの共通的な項目
  97  	var tmp alias
  98  	if err := json.Unmarshal(data, &tmp); err != nil {
  99  		return err
 100  	}
 101  
 102  	// ラッパーが省略されている、レスポンスボディの直下にある項目
 103  	var result naked.CertificateAuthorityAddClientOrServerResult
 104  	if err := json.Unmarshal(data, &result); err != nil {
 105  		return err
 106  	}
 107  	tmp.CertificateAuthority = &result
 108  
 109  	*a = certificateAuthorityAddServerResponseEnvelope(tmp)
 110  	return nil
 111  }
 112  
 113  func (m *mobileGatewaySetSIMRoutesRequestEnvelope) MarshalJSON() ([]byte, error) {
 114  	type alias struct {
 115  		SIMRoutes []*naked.MobileGatewaySIMRoute `json:"sim_routes"`
 116  	}
 117  	tmp := &alias{
 118  		SIMRoutes: m.SIMRoutes,
 119  	}
 120  	if len(tmp.SIMRoutes) == 0 {
 121  		tmp.SIMRoutes = make([]*naked.MobileGatewaySIMRoute, 0)
 122  	}
 123  	return json.Marshal(tmp)
 124  }
 125  
 126  // UnmarshalJSON APIからの戻り値でレスポンスボディ直下にデータを持つことへの対応
 127  func (s *serverGetVNCProxyResponseEnvelope) UnmarshalJSON(data []byte) error {
 128  	type alias serverGetVNCProxyResponseEnvelope
 129  
 130  	var tmp alias
 131  	if err := json.Unmarshal(data, &tmp); err != nil {
 132  		return err
 133  	}
 134  
 135  	var nakedVNCProxy naked.VNCProxyInfo
 136  	if err := json.Unmarshal(data, &nakedVNCProxy); err != nil {
 137  		return err
 138  	}
 139  	tmp.VNCProxyInfo = &nakedVNCProxy
 140  
 141  	*s = serverGetVNCProxyResponseEnvelope(tmp)
 142  	return nil
 143  }
 144  
 145  /*
 146   * 検索時に固定パラメータを設定するための実装
 147   */
 148  
 149  func (s autoBackupFindRequestEnvelope) MarshalJSON() ([]byte, error) {
 150  	type alias autoBackupFindRequestEnvelope
 151  	tmp := alias(s)
 152  	if tmp.Filter == nil {
 153  		tmp.Filter = search.Filter{}
 154  	}
 155  	tmp.Filter[search.Key("Provider.Class")] = "autobackup"
 156  	return json.Marshal(tmp)
 157  }
 158  
 159  func (s autoScaleFindRequestEnvelope) MarshalJSON() ([]byte, error) {
 160  	type alias autoScaleFindRequestEnvelope
 161  	tmp := alias(s)
 162  	if tmp.Filter == nil {
 163  		tmp.Filter = search.Filter{}
 164  	}
 165  	tmp.Filter[search.Key("Provider.Class")] = "autoscale"
 166  	return json.Marshal(tmp)
 167  }
 168  
 169  func (s certificateAuthorityFindRequestEnvelope) MarshalJSON() ([]byte, error) {
 170  	type alias certificateAuthorityFindRequestEnvelope
 171  	tmp := alias(s)
 172  	if tmp.Filter == nil {
 173  		tmp.Filter = search.Filter{}
 174  	}
 175  	tmp.Filter[search.Key("Provider.Class")] = "certificateauthority"
 176  	return json.Marshal(tmp)
 177  }
 178  
 179  func (s containerRegistryFindRequestEnvelope) MarshalJSON() ([]byte, error) {
 180  	type alias containerRegistryFindRequestEnvelope
 181  	tmp := alias(s)
 182  	if tmp.Filter == nil {
 183  		tmp.Filter = search.Filter{}
 184  	}
 185  	tmp.Filter[search.Key("Provider.Class")] = "containerregistry"
 186  	return json.Marshal(tmp)
 187  }
 188  
 189  func (s eSMEFindRequestEnvelope) MarshalJSON() ([]byte, error) {
 190  	type alias eSMEFindRequestEnvelope
 191  	tmp := alias(s)
 192  	if tmp.Filter == nil {
 193  		tmp.Filter = search.Filter{}
 194  	}
 195  	tmp.Filter[search.Key("Provider.Class")] = "esme"
 196  	return json.Marshal(tmp)
 197  }
 198  
 199  func (s dNSFindRequestEnvelope) MarshalJSON() ([]byte, error) {
 200  	type alias dNSFindRequestEnvelope
 201  	tmp := alias(s)
 202  	if tmp.Filter == nil {
 203  		tmp.Filter = search.Filter{}
 204  	}
 205  	tmp.Filter[search.Key("Provider.Class")] = "dns"
 206  	return json.Marshal(tmp)
 207  }
 208  
 209  func (s simpleMonitorFindRequestEnvelope) MarshalJSON() ([]byte, error) {
 210  	type alias simpleMonitorFindRequestEnvelope
 211  	tmp := alias(s)
 212  	if tmp.Filter == nil {
 213  		tmp.Filter = search.Filter{}
 214  	}
 215  	tmp.Filter[search.Key("Provider.Class")] = "simplemon"
 216  	return json.Marshal(tmp)
 217  }
 218  
 219  func (s simpleNotificationDestinationFindRequestEnvelope) MarshalJSON() ([]byte, error) {
 220  	type alias simpleNotificationDestinationFindRequestEnvelope
 221  	tmp := alias(s)
 222  	if tmp.Filter == nil {
 223  		tmp.Filter = search.Filter{}
 224  	}
 225  	tmp.Filter[search.Key("Provider.Class")] = "saknoticedestination"
 226  	return json.Marshal(tmp)
 227  }
 228  
 229  func (s simpleNotificationGroupFindRequestEnvelope) MarshalJSON() ([]byte, error) {
 230  	type alias simpleNotificationGroupFindRequestEnvelope
 231  	tmp := alias(s)
 232  	if tmp.Filter == nil {
 233  		tmp.Filter = search.Filter{}
 234  	}
 235  	tmp.Filter[search.Key("Provider.Class")] = "saknoticegroup"
 236  	return json.Marshal(tmp)
 237  }
 238  
 239  func (s gSLBFindRequestEnvelope) MarshalJSON() ([]byte, error) {
 240  	type alias gSLBFindRequestEnvelope
 241  	tmp := alias(s)
 242  	if tmp.Filter == nil {
 243  		tmp.Filter = search.Filter{}
 244  	}
 245  	tmp.Filter[search.Key("Provider.Class")] = "gslb"
 246  	return json.Marshal(tmp)
 247  }
 248  
 249  func (s proxyLBFindRequestEnvelope) MarshalJSON() ([]byte, error) {
 250  	type alias proxyLBFindRequestEnvelope
 251  	tmp := alias(s)
 252  	if tmp.Filter == nil {
 253  		tmp.Filter = search.Filter{}
 254  	}
 255  	tmp.Filter[search.Key("Provider.Class")] = "proxylb"
 256  	return json.Marshal(tmp)
 257  }
 258  
 259  func (s sIMFindRequestEnvelope) MarshalJSON() ([]byte, error) {
 260  	type alias sIMFindRequestEnvelope
 261  	tmp := alias(s)
 262  	if tmp.Filter == nil {
 263  		tmp.Filter = search.Filter{}
 264  	}
 265  	tmp.Filter[search.Key("Provider.Class")] = "sim"
 266  	return json.Marshal(tmp)
 267  }
 268  
 269  func (s localRouterFindRequestEnvelope) MarshalJSON() ([]byte, error) {
 270  	type alias localRouterFindRequestEnvelope
 271  	tmp := alias(s)
 272  	if tmp.Filter == nil {
 273  		tmp.Filter = search.Filter{}
 274  	}
 275  	tmp.Filter[search.Key("Provider.Class")] = "localrouter"
 276  	return json.Marshal(tmp)
 277  }
 278  
 279  func (s enhancedDBFindRequestEnvelope) MarshalJSON() ([]byte, error) {
 280  	type alias enhancedDBFindRequestEnvelope
 281  	tmp := alias(s)
 282  	if tmp.Filter == nil {
 283  		tmp.Filter = search.Filter{}
 284  	}
 285  	tmp.Filter[search.Key("Provider.Class")] = "enhanceddb"
 286  	return json.Marshal(tmp)
 287  }
 288  
 289  func (s databaseFindRequestEnvelope) MarshalJSON() ([]byte, error) {
 290  	type alias databaseFindRequestEnvelope
 291  	tmp := alias(s)
 292  	if tmp.Filter == nil {
 293  		tmp.Filter = search.Filter{}
 294  	}
 295  	tmp.Filter[search.Key("Class")] = "database"
 296  	return json.Marshal(tmp)
 297  }
 298  
 299  func (s loadBalancerFindRequestEnvelope) MarshalJSON() ([]byte, error) {
 300  	type alias loadBalancerFindRequestEnvelope
 301  	tmp := alias(s)
 302  	if tmp.Filter == nil {
 303  		tmp.Filter = search.Filter{}
 304  	}
 305  	tmp.Filter[search.Key("Class")] = "loadbalancer"
 306  	return json.Marshal(tmp)
 307  }
 308  
 309  func (s vPCRouterFindRequestEnvelope) MarshalJSON() ([]byte, error) {
 310  	type alias vPCRouterFindRequestEnvelope
 311  	tmp := alias(s)
 312  	if tmp.Filter == nil {
 313  		tmp.Filter = search.Filter{}
 314  	}
 315  	tmp.Filter[search.Key("Class")] = "vpcrouter"
 316  	return json.Marshal(tmp)
 317  }
 318  
 319  func (s nFSFindRequestEnvelope) MarshalJSON() ([]byte, error) {
 320  	type alias nFSFindRequestEnvelope
 321  	tmp := alias(s)
 322  	if tmp.Filter == nil {
 323  		tmp.Filter = search.Filter{}
 324  	}
 325  	tmp.Filter[search.Key("Class")] = "nfs"
 326  	return json.Marshal(tmp)
 327  }
 328  
 329  func (s mobileGatewayFindRequestEnvelope) MarshalJSON() ([]byte, error) {
 330  	type alias mobileGatewayFindRequestEnvelope
 331  	tmp := alias(s)
 332  	if tmp.Filter == nil {
 333  		tmp.Filter = search.Filter{}
 334  	}
 335  	tmp.Filter[search.Key("Class")] = "mobilegateway"
 336  	return json.Marshal(tmp)
 337  }
 338  
 339  /*
 340   * for Shared Archive
 341   */
 342  
 343  func (s archiveShareRequestEnvelope) MarshalJSON() ([]byte, error) {
 344  	type alias archiveShareRequestEnvelope
 345  	tmp := alias(s)
 346  	tmp.Shared = true
 347  	return json.Marshal(tmp)
 348  }
 349  
 350  // UnmarshalJSON APIからの戻り値でレスポンスボディ直下にデータを持つことへの対応
 351  func (s *archiveShareResponseEnvelope) UnmarshalJSON(data []byte) error {
 352  	type alias archiveShareResponseEnvelope
 353  
 354  	var tmp alias
 355  	if err := json.Unmarshal(data, &tmp); err != nil {
 356  		return err
 357  	}
 358  
 359  	var nakedData naked.ArchiveShareInfo
 360  	if err := json.Unmarshal(data, &nakedData); err != nil {
 361  		return err
 362  	}
 363  	tmp.ArchiveShareInfo = &nakedData
 364  
 365  	*s = archiveShareResponseEnvelope(tmp)
 366  	return nil
 367  }
 368  
 369  // UnmarshalJSON APIからの戻り値でレスポンスボディ直下にデータを持つことへの対応
 370  func (a *vPCRouterLogsResponseEnvelope) UnmarshalJSON(data []byte) error {
 371  	type alias vPCRouterLogsResponseEnvelope
 372  
 373  	var tmp alias
 374  	if err := json.Unmarshal(data, &tmp); err != nil {
 375  		return err
 376  	}
 377  
 378  	var nakedLogs naked.VPCRouterLog
 379  	if err := json.Unmarshal(data, &nakedLogs); err != nil {
 380  		return err
 381  	}
 382  	tmp.VPCRouter = &nakedLogs
 383  
 384  	*a = vPCRouterLogsResponseEnvelope(tmp)
 385  	return nil
 386  }
 387  
 388  // UnmarshalJSON APIからの戻り値でレスポンスボディ直下にデータを持つことへの対応
 389  func (a *vPCRouterPingResponseEnvelope) UnmarshalJSON(data []byte) error {
 390  	type alias vPCRouterPingResponseEnvelope
 391  
 392  	var tmp alias
 393  	if err := json.Unmarshal(data, &tmp); err != nil {
 394  		return err
 395  	}
 396  
 397  	var nakedResult naked.VPCRouterPingResult
 398  	if err := json.Unmarshal(data, &nakedResult); err != nil {
 399  		return err
 400  	}
 401  	tmp.VPCRouter = &nakedResult
 402  
 403  	*a = vPCRouterPingResponseEnvelope(tmp)
 404  	return nil
 405  }
 406  
 407  // UnmarshalJSON APIからの戻り値でレスポンスボディ直下にデータを持つことへの対応
 408  func (a *simpleNotificationGroupHistoryResponseEnvelope) UnmarshalJSON(data []byte) error {
 409  	var nakedResult naked.SimpleNotificationHistories
 410  	if err := json.Unmarshal(data, &nakedResult); err != nil {
 411  		return err
 412  	}
 413  
 414  	a.NotificationHistories = &nakedResult
 415  	return nil
 416  }
 417