|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "encoding/json" |
5 | 4 | "log" |
6 | 5 | "os" |
7 | 6 |
|
8 | | - "github.com/scaleway/scaleway-sdk-go/api/k8s/v1" |
9 | | - "github.com/scaleway/scaleway-sdk-go/api/rdb/v1" |
10 | | - "gopkg.in/dnaeon/go-vcr.v3/cassette" |
| 7 | + "github.com/scaleway/terraform-provider-scaleway/v2/internal/acctest" |
11 | 8 | ) |
12 | 9 |
|
13 | | -var transientStates = map[string]bool{ |
14 | | - k8s.ClusterStatusCreating.String(): true, |
15 | | - k8s.ClusterStatusDeleting.String(): true, |
16 | | - k8s.ClusterStatusUpdating.String(): true, |
17 | | - k8s.PoolStatusDeleting.String(): true, |
18 | | - k8s.PoolStatusScaling.String(): true, |
19 | | - k8s.PoolStatusUpgrading.String(): true, |
20 | | - |
21 | | - rdb.DatabaseBackupStatusCreating.String(): true, |
22 | | - rdb.DatabaseBackupStatusDeleting.String(): true, |
23 | | - rdb.DatabaseBackupStatusExporting.String(): true, |
24 | | - rdb.DatabaseBackupStatusRestoring.String(): true, |
25 | | - rdb.InstanceStatusAutohealing.String(): true, |
26 | | - rdb.InstanceStatusBackuping.String(): true, |
27 | | - rdb.InstanceStatusConfiguring.String(): true, |
28 | | - rdb.InstanceStatusDeleting.String(): true, |
29 | | - rdb.InstanceStatusInitializing.String(): true, |
30 | | - rdb.InstanceStatusProvisioning.String(): true, |
31 | | - rdb.InstanceStatusRestarting.String(): true, |
32 | | - rdb.InstanceStatusSnapshotting.String(): true, |
33 | | -} |
34 | | - |
35 | 10 | func main() { |
36 | 11 | if len(os.Args) < 2 { |
37 | 12 | log.Fatalf("Usage: %s <cassette_file_name_without_yaml>\n", os.Args[0]) |
38 | 13 | } |
39 | 14 |
|
40 | 15 | path := os.Args[1] |
41 | 16 |
|
42 | | - inputCassette, err := cassette.Load(path) |
| 17 | + report, err := acctest.CompressCassette(path) |
43 | 18 | if err != nil { |
44 | | - log.Fatalf("Error while reading file : %v\n", err) |
| 19 | + log.Fatalf("%s", err) |
45 | 20 | } |
46 | 21 |
|
47 | | - outputCassette := cassette.New(path) |
48 | | - transitioning := false |
49 | | - |
50 | | - for i := range len(inputCassette.Interactions) { |
51 | | - interaction := inputCassette.Interactions[i] |
52 | | - responseBody := interaction.Response.Body |
53 | | - requestMethod := interaction.Request.Method |
54 | | - |
55 | | - if requestMethod != "GET" { |
56 | | - transitioning = false |
57 | | - |
58 | | - log.Printf("Interaction %d is not a GET request. Recording it\n", i) |
59 | | - outputCassette.AddInteraction(interaction) |
60 | | - |
61 | | - continue |
62 | | - } |
63 | | - |
64 | | - if responseBody == "" { |
65 | | - log.Printf("Interaction %d got an empty response body. Recording it\n", i) |
66 | | - outputCassette.AddInteraction(interaction) |
67 | | - |
68 | | - continue |
69 | | - } |
70 | | - |
71 | | - var m map[string]any |
72 | | - |
73 | | - err := json.Unmarshal([]byte(responseBody), &m) |
74 | | - if err != nil { |
75 | | - log.Printf("Interaction %d have an error with unmarshalling response body: %v. Recording it\n", i, err) |
76 | | - outputCassette.AddInteraction(interaction) |
77 | | - |
78 | | - continue |
79 | | - } |
80 | | - |
81 | | - if m["status"] == nil { |
82 | | - log.Printf("Interaction %d does not contain a status field. Recording it\n", i) |
83 | | - outputCassette.AddInteraction(interaction) |
84 | | - |
85 | | - continue |
86 | | - } |
87 | | - |
88 | | - status := m["status"].(string) |
89 | | - // We test if the state is transient |
90 | | - if _, ok := transientStates[status]; ok { |
91 | | - if transitioning { |
92 | | - log.Printf("Interaction %d is in a transient state while we are already in transitient state. No need to record it: %s\n", i, status) |
93 | | - } else { |
94 | | - log.Printf("Interaction %d is in a transient state: %s, Recording it\n", i, status) |
95 | | - |
96 | | - transitioning = true |
97 | | - |
98 | | - outputCassette.AddInteraction(interaction) |
99 | | - } |
100 | | - } else { |
101 | | - if transitioning { |
102 | | - log.Printf("Interaction %d is not in a transient state anymore: %s, Recording it\n", i, status) |
103 | | - |
104 | | - outputCassette.AddInteraction(interaction) |
105 | | - |
106 | | - transitioning = false |
107 | | - } else { |
108 | | - log.Printf("Interaction %d is not in a transient state: %s, Recording it\n", i, status) |
109 | | - outputCassette.AddInteraction(interaction) |
110 | | - } |
111 | | - } |
112 | | - } |
113 | | - |
114 | | - err = outputCassette.Save() |
115 | | - if err != nil { |
116 | | - log.Fatalf("error while saving file: %v", err) |
117 | | - } |
| 22 | + report.Print() |
118 | 23 | } |
0 commit comments