Skip to content

Commit 5ffecf4

Browse files
Engin Dirialexellis
Engin Diri
authored andcommitted
Add delete by IP on OVHcloud provisioner
Signed-off-by: Engin Diri <[email protected]>
1 parent a67b248 commit 5ffecf4

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

provision/ovh.go

+26-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package provision
22

33
import (
44
"context"
5+
"fmt"
56
ovhsdk "github.com/dirien/ovh-go-sdk/pkg/sdk"
67
)
78

@@ -75,8 +76,32 @@ func (o *OVHProvisioner) Status(id string) (*ProvisionedHost, error) {
7576
}, nil
7677
}
7778

79+
func (o *OVHProvisioner) lookupID(request HostDeleteRequest) (string, error) {
80+
instances, err := o.client.ListInstance(context.Background())
81+
if err != nil {
82+
return "", err
83+
}
84+
for _, instance := range instances {
85+
ip4, _ := ovhsdk.IPv4(&instance)
86+
if ip4 == request.IP {
87+
return instance.ID, nil
88+
}
89+
}
90+
return "", fmt.Errorf("no host with ip: %s", request.IP)
91+
}
92+
7893
func (o *OVHProvisioner) Delete(request HostDeleteRequest) error {
79-
err := o.client.DeleteInstance(context.Background(), request.ID)
94+
var id string
95+
var err error
96+
if len(request.ID) > 0 {
97+
id = request.ID
98+
} else {
99+
id, err = o.lookupID(request)
100+
if err != nil {
101+
return err
102+
}
103+
}
104+
err = o.client.DeleteInstance(context.Background(), id)
80105
if err != nil {
81106
return err
82107
}

0 commit comments

Comments
 (0)