Skip to content

Commit ad9394c

Browse files
authored
feat(ipfs): add export and import key (#1826)
1 parent cf5147e commit ad9394c

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

api/ipfs/v1alpha1/ipfs_sdk.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,20 @@ func (enum *PinStatus) UnmarshalJSON(data []byte) error {
256256
return nil
257257
}
258258

259+
type ExportKeyNameResponse struct {
260+
NameID string `json:"name_id"`
261+
262+
ProjectID string `json:"project_id"`
263+
264+
CreatedAt *time.Time `json:"created_at"`
265+
266+
UpdatedAt *time.Time `json:"updated_at"`
267+
268+
PublicKey string `json:"public_key"`
269+
270+
PrivateKey string `json:"private_key"`
271+
}
272+
259273
type ListNamesResponse struct {
260274
Names []*Name `json:"names"`
261275

@@ -1144,6 +1158,92 @@ func (s *API) UpdateName(req *UpdateNameRequest, opts ...scw.RequestOption) (*Na
11441158
return &resp, nil
11451159
}
11461160

1161+
type ExportKeyNameRequest struct {
1162+
// Region: region to target. If none is passed will use default region from the config.
1163+
Region scw.Region `json:"-"`
1164+
1165+
NameID string `json:"-"`
1166+
}
1167+
1168+
func (s *API) ExportKeyName(req *ExportKeyNameRequest, opts ...scw.RequestOption) (*ExportKeyNameResponse, error) {
1169+
var err error
1170+
1171+
if req.Region == "" {
1172+
defaultRegion, _ := s.client.GetDefaultRegion()
1173+
req.Region = defaultRegion
1174+
}
1175+
1176+
if fmt.Sprint(req.Region) == "" {
1177+
return nil, errors.New("field Region cannot be empty in request")
1178+
}
1179+
1180+
if fmt.Sprint(req.NameID) == "" {
1181+
return nil, errors.New("field NameID cannot be empty in request")
1182+
}
1183+
1184+
scwReq := &scw.ScalewayRequest{
1185+
Method: "GET",
1186+
Path: "/ipfs/v1alpha1/regions/" + fmt.Sprint(req.Region) + "/names/export-key/" + fmt.Sprint(req.NameID) + "",
1187+
Headers: http.Header{},
1188+
}
1189+
1190+
var resp ExportKeyNameResponse
1191+
1192+
err = s.client.Do(scwReq, &resp, opts...)
1193+
if err != nil {
1194+
return nil, err
1195+
}
1196+
return &resp, nil
1197+
}
1198+
1199+
type ImportKeyNameRequest struct {
1200+
// Region: region to target. If none is passed will use default region from the config.
1201+
Region scw.Region `json:"-"`
1202+
1203+
ProjectID string `json:"project_id"`
1204+
1205+
Name string `json:"name"`
1206+
1207+
PrivateKey string `json:"private_key"`
1208+
}
1209+
1210+
func (s *API) ImportKeyName(req *ImportKeyNameRequest, opts ...scw.RequestOption) (*Name, error) {
1211+
var err error
1212+
1213+
if req.ProjectID == "" {
1214+
defaultProjectID, _ := s.client.GetDefaultProjectID()
1215+
req.ProjectID = defaultProjectID
1216+
}
1217+
1218+
if req.Region == "" {
1219+
defaultRegion, _ := s.client.GetDefaultRegion()
1220+
req.Region = defaultRegion
1221+
}
1222+
1223+
if fmt.Sprint(req.Region) == "" {
1224+
return nil, errors.New("field Region cannot be empty in request")
1225+
}
1226+
1227+
scwReq := &scw.ScalewayRequest{
1228+
Method: "POST",
1229+
Path: "/ipfs/v1alpha1/regions/" + fmt.Sprint(req.Region) + "/names/import-key",
1230+
Headers: http.Header{},
1231+
}
1232+
1233+
err = scwReq.SetBody(req)
1234+
if err != nil {
1235+
return nil, err
1236+
}
1237+
1238+
var resp Name
1239+
1240+
err = s.client.Do(scwReq, &resp, opts...)
1241+
if err != nil {
1242+
return nil, err
1243+
}
1244+
return &resp, nil
1245+
}
1246+
11471247
// UnsafeGetTotalCount should not be used
11481248
// Internal usage only
11491249
func (r *ListVolumesResponse) UnsafeGetTotalCount() uint64 {

0 commit comments

Comments
 (0)