github.com/sacloud/libsacloud/v2@v2.32.3/internal/tools/gen-api-fake-data/main.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 main
    16  
    17  import (
    18  	"context"
    19  	"log"
    20  	"path/filepath"
    21  
    22  	"github.com/sacloud/libsacloud/v2/helper/query"
    23  	"github.com/sacloud/libsacloud/v2/internal/tools"
    24  	"github.com/sacloud/libsacloud/v2/sacloud"
    25  	"github.com/sacloud/libsacloud/v2/sacloud/ostype"
    26  	"github.com/sacloud/libsacloud/v2/sacloud/search"
    27  	"github.com/sacloud/libsacloud/v2/sacloud/search/keys"
    28  	"github.com/sacloud/libsacloud/v2/sacloud/types"
    29  )
    30  
    31  func init() {
    32  	log.SetFlags(0)
    33  	log.SetPrefix("gen-api-fake-data: ")
    34  }
    35  
    36  var fakeDataDefines = []struct {
    37  	destination   string
    38  	template      string
    39  	parameterFunc func() interface{}
    40  }{
    41  	{
    42  		destination:   "sacloud/fake/zz_init_archive.go",
    43  		template:      tmplArchive,
    44  		parameterFunc: collectArchives,
    45  	},
    46  	{
    47  		destination:   "sacloud/fake/zz_init_cdrom.go",
    48  		template:      tmplCDROM,
    49  		parameterFunc: collectCDROMs,
    50  	},
    51  }
    52  
    53  func main() {
    54  	for _, generator := range fakeDataDefines {
    55  		param := generator.parameterFunc()
    56  		tools.WriteFileWithTemplate(&tools.TemplateConfig{
    57  			OutputPath: filepath.Join(tools.ProjectRootPath(), generator.destination),
    58  			Template:   generator.template,
    59  			Parameter:  param,
    60  		})
    61  		log.Printf("generated: %s\n", filepath.Join(generator.destination))
    62  	}
    63  }
    64  
    65  func collectArchives() interface{} {
    66  	caller, err := sacloud.NewClientFromEnv()
    67  	if err != nil {
    68  		log.Fatal(err)
    69  	}
    70  	tmplParam := map[string][]*sacloud.Archive{}
    71  	archiveOp := sacloud.NewArchiveOp(caller)
    72  	ctx := context.Background()
    73  
    74  	for _, zone := range sacloud.SakuraCloudZones {
    75  		var archives []*sacloud.Archive
    76  		for _, ost := range ostype.ArchiveOSTypes {
    77  			archive, err := query.FindArchiveByOSType(ctx, archiveOp, zone, ost)
    78  			if err != nil {
    79  				log.Fatal(err)
    80  			}
    81  			archives = append(archives, archive)
    82  		}
    83  		tmplParam[zone] = archives
    84  	}
    85  	return tmplParam
    86  }
    87  
    88  const tmplArchive = `// generated by 'github.com/sacloud/libsacloud/internal/tools/gen-api-fake-data'; DO NOT EDIT
    89  
    90  package fake
    91  
    92  import (
    93  	"github.com/sacloud/libsacloud/v2/sacloud"
    94  	"github.com/sacloud/libsacloud/v2/sacloud/types"
    95  )
    96  
    97  var initArchives = map[string][]*sacloud.Archive{
    98  {{ range $zone, $archives := . -}}
    99  	"{{$zone}}": {
   100  {{ range $archives -}}
   101  		{
   102  			ID:                   types.ID({{.ID}}),
   103  			Name:                 "{{.Name}}",
   104  			Description:          "fake",
   105  			Tags:                 types.Tags{ {{range .Tags}}"{{.}}",{{ end }} },
   106  			DisplayOrder:         {{.DisplayOrder}},
   107  			Availability:         types.EAvailability("{{.Availability}}"),
   108  			Scope:                types.EScope("{{.Scope}}"),
   109  			SizeMB:               {{.SizeMB}},
   110  			DiskPlanID:           types.ID({{.DiskPlanID}}),
   111  			DiskPlanName:         "{{.DiskPlanName}}",
   112  			DiskPlanStorageClass: "{{.DiskPlanStorageClass}}",
   113  		},
   114  {{ end -}}
   115  	},
   116  {{ end -}}
   117  }
   118  `
   119  
   120  func collectCDROMs() interface{} {
   121  	caller, err := sacloud.NewClientFromEnv()
   122  	if err != nil {
   123  		log.Fatal(err)
   124  	}
   125  	tmplParam := map[string][]*sacloud.CDROM{}
   126  	cdromOp := sacloud.NewCDROMOp(caller)
   127  	ctx := context.Background()
   128  
   129  	for _, zone := range sacloud.SakuraCloudZones {
   130  		var cdroms []*sacloud.CDROM
   131  
   132  		searched, err := cdromOp.Find(ctx, zone, &sacloud.FindCondition{
   133  			Filter: search.Filter{
   134  				search.Key(keys.Scope): string(types.Scopes.Shared),
   135  			},
   136  		})
   137  		if err != nil {
   138  			log.Fatal(err)
   139  		}
   140  
   141  		cdroms = append(cdroms, searched.CDROMs...)
   142  		tmplParam[zone] = cdroms
   143  	}
   144  	return tmplParam
   145  }
   146  
   147  const tmplCDROM = `// generated by 'github.com/sacloud/libsacloud/internal/tools/gen-api-fake-data'; DO NOT EDIT
   148  
   149  package fake
   150  
   151  import (
   152  	"github.com/sacloud/libsacloud/v2/sacloud"
   153  	"github.com/sacloud/libsacloud/v2/sacloud/types"
   154  )
   155  
   156  var initCDROM = map[string][]*sacloud.CDROM{
   157  {{ range $zone, $data := . -}}
   158  	"{{$zone}}": {
   159  {{ range $data -}}
   160  		{
   161  			ID:                   types.ID({{.ID}}),
   162  			Name:                 "{{.Name}}",
   163  			Description:          "fake",
   164  			Tags:                 types.Tags{ {{range .Tags}}"{{.}}",{{ end }} },
   165  			DisplayOrder:         {{.DisplayOrder}},
   166  			Availability:         types.EAvailability("{{.Availability}}"),
   167  			Scope:                types.EScope("{{.Scope}}"),
   168  			SizeMB:               {{.SizeMB}},
   169  		},
   170  {{ end -}}
   171  	},
   172  {{ end -}}
   173  }
   174  `