File: marketplace_utils.go

package info (click to toggle)
golang-github-scaleway-scaleway-sdk-go 1.0.0~beta32-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,444 kB
  • sloc: sh: 70; makefile: 3
file content (85 lines) | stat: -rw-r--r-- 2,507 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package marketplace

import (
	"strings"

	"github.com/scaleway/scaleway-sdk-go/errors"
	"github.com/scaleway/scaleway-sdk-go/scw"
)

// FindByLabel returns the first image with the given label in the image list
// Cannot find an image if it is not in the ListImagesResponse struct
// Use scw.WithAllPages when listing image to get all images
func (r *ListImagesResponse) FindByLabel(label string) *Image {
	for _, image := range r.Images {
		if image.Label == label {
			return image
		}
	}
	return nil
}

type GetImageByLabelRequest struct {
	Label string
}

// GetImageByLabel returns the image with the given label
func (s *API) GetImageByLabel(req *GetImageByLabelRequest, opts ...scw.RequestOption) (*Image, error) {
	listImagesRequest := &ListImagesRequest{}
	opts = append(opts, scw.WithAllPages())

	listImagesResponse, err := s.ListImages(listImagesRequest, opts...)
	if err != nil {
		return nil, err
	}

	image := listImagesResponse.FindByLabel(req.Label)
	if image == nil {
		return nil, errors.New("couldn't find a matching image for the given label (%s)", req.Label)
	}

	return image, nil
}

type GetLocalImageByLabelRequest struct {
	ImageLabel     string
	Zone           scw.Zone
	CommercialType string
	Type           LocalImageType
}

// GetLocalImageByLabel returns the local image for the given image label in the given zone and compatible with given commercial type
func (s *API) GetLocalImageByLabel(req *GetLocalImageByLabelRequest, opts ...scw.RequestOption) (*LocalImage, error) {
	if req.Zone == "" {
		defaultZone, _ := s.client.GetDefaultZone()
		req.Zone = defaultZone
	}
	req.CommercialType = strings.ToUpper(req.CommercialType)

	resp, err := s.ListLocalImages(&ListLocalImagesRequest{
		ImageLabel: scw.StringPtr(req.ImageLabel),
		Zone:       &req.Zone,
		Type:       req.Type,
	}, opts...)
	if err != nil {
		return nil, err
	}
	for _, localImage := range resp.LocalImages {
		if localImage.IsCompatible(req.CommercialType) {
			return localImage, nil
		}
	}

	return nil, errors.New("couldn't find a local image for the given zone (%s) and commercial type (%s)", req.Zone, req.CommercialType)
}

// IsCompatible returns true if a local image is compatible with the given instance type
// commercialType should be an uppercase string ex: DEV1-S
func (li *LocalImage) IsCompatible(commercialType string) bool {
	for _, compatibleCommercialType := range li.CompatibleCommercialTypes {
		if compatibleCommercialType == commercialType {
			return true
		}
	}
	return false
}