/
asset.go
164 lines (149 loc) · 4.9 KB
/
asset.go
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright 2023 VMware, Inc.
// SPDX-License-Identifier: BSD-2-Clause
package pkg
import (
"fmt"
"github.com/vmware-labs/marketplace-cli/v2/internal/models"
)
type Asset struct {
DisplayName string `json:"displayname"`
Filename string `json:"filename"`
Version string `json:"version"`
Type string `json:"type"`
Size int64 `json:"size"`
Downloadable bool `json:"downloadable"`
Downloads int64 `json:"downloads"`
DownloadRequestPayload *DownloadRequestPayload `json:"-"`
Error string `json:"error,omitempty"`
Status string `json:"status,omitempty"`
}
const (
AssetTypeOther = "Other"
AssetTypeVM = "VM"
AssetTypeChart = "Chart"
AssetTypeContainerImage = "Container Image"
AssetTypeMetaFile = "MetaFile"
)
func GetAssets(product *models.Product, version string) []*Asset {
var assets []*Asset
if !product.HasVersion(version) {
return assets
}
for _, otherFile := range product.GetAddonFilesForVersion(version) {
assets = append(assets, &Asset{
DisplayName: otherFile.Name,
Filename: otherFile.Name,
Version: version,
Type: AssetTypeOther,
Size: otherFile.Size,
Downloads: otherFile.DownloadCount,
Downloadable: otherFile.Status != models.DeploymentStatusInactive,
DownloadRequestPayload: &DownloadRequestPayload{
ProductId: product.ProductId,
AppVersion: version,
IsAddonFile: true,
AddonFileId: otherFile.ID,
},
Error: otherFile.DeploymentStatus,
Status: otherFile.Status,
})
}
for _, file := range product.GetFilesForVersion(version) {
assets = append(assets, &Asset{
DisplayName: file.Name,
Filename: file.Name,
Version: version,
Type: AssetTypeVM,
Size: file.CalculateSize(),
Downloads: file.DownloadCount,
Downloadable: file.Status != models.DeploymentStatusInactive,
DownloadRequestPayload: &DownloadRequestPayload{
ProductId: product.ProductId,
AppVersion: version,
DeploymentFileId: file.FileID,
},
Error: file.Comment,
Status: file.Status,
})
}
for _, chart := range product.GetChartsForVersion(version) {
assets = append(assets, &Asset{
DisplayName: chart.HelmTarUrl,
Filename: "chart.tgz",
Version: chart.Version,
Type: AssetTypeChart,
Size: chart.Size,
Downloadable: chart.IsUpdatedInMarketplaceRegistry,
Downloads: chart.DownloadCount,
DownloadRequestPayload: &DownloadRequestPayload{
ProductId: product.ProductId,
AppVersion: version,
ChartVersion: chart.Version,
},
Error: chart.ProcessingError,
Status: chart.Status,
})
}
containerImages := product.GetContainerImagesForVersion(version)
if len(containerImages) > 0 {
for _, containerImage := range containerImages {
for _, imageURL := range containerImage.DockerURLs {
for _, tag := range imageURL.ImageTags {
assets = append(assets, &Asset{
DisplayName: fmt.Sprintf("%s:%s", imageURL.Url, tag.Tag),
Filename: "image.tar",
Version: tag.Tag,
Type: AssetTypeContainerImage,
Size: tag.Size,
Downloads: tag.DownloadCount,
Downloadable: tag.IsUpdatedInMarketplaceRegistry,
DownloadRequestPayload: &DownloadRequestPayload{
ProductId: product.ProductId,
AppVersion: version,
DockerlinkVersionID: containerImage.ID,
DockerUrlId: imageURL.ID,
ImageTagId: tag.ID,
},
Error: tag.ProcessingError,
Status: containerImage.Status,
})
}
}
}
}
for _, metafile := range product.GetMetaFilesForVersion(version) {
for _, object := range metafile.Objects {
assets = append(assets, &Asset{
DisplayName: object.FileName,
Filename: object.FileName,
Version: metafile.Version,
Type: AssetTypeMetaFile,
Size: object.Size,
Downloads: object.DownloadCount,
Downloadable: object.IsFileBackedUp, // Is this valid?
DownloadRequestPayload: &DownloadRequestPayload{
ProductId: product.ProductId,
AppVersion: version,
MetaFileID: metafile.ID,
MetaFileObjectID: object.FileID,
},
Error: object.ProcessingError,
Status: metafile.Status,
})
}
}
return assets
}
func GetAssetsByType(assetType string, product *models.Product, version string) []*Asset {
var filteredAssets []*Asset
unfilteredAssets := GetAssets(product, version)
if assetType == "" {
return unfilteredAssets
}
for _, asset := range unfilteredAssets {
if asset.Type == assetType {
filteredAssets = append(filteredAssets, asset)
}
}
return filteredAssets
}