-
Notifications
You must be signed in to change notification settings - Fork 8
/
project.go
121 lines (99 loc) · 2.68 KB
/
project.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
// Copyright (C) 2020 Storj Labs, Inc.
// See LICENSE for copying information.
package main
// #include "uplink_definitions.h"
import "C"
import (
"unsafe"
"storj.io/uplink"
)
// Project provides access to managing buckets.
type Project struct {
scope
*uplink.Project
}
// uplink_open_project opens project using access grant.
//
//export uplink_open_project
func uplink_open_project(access *C.UplinkAccess) C.UplinkProjectResult {
if access == nil {
return C.UplinkProjectResult{
error: mallocError(ErrNull.New("access")),
}
}
acc, ok := universe.Get(access._handle).(*Access)
if !ok {
return C.UplinkProjectResult{
error: mallocError(ErrInvalidHandle.New("Access")),
}
}
scope := rootScope("")
config := uplink.Config{}
proj, err := config.OpenProject(scope.ctx, acc.Access)
if err != nil {
return C.UplinkProjectResult{
error: mallocError(err),
}
}
return C.UplinkProjectResult{
project: (*C.UplinkProject)(mallocHandle(universe.Add(&Project{scope, proj}))),
}
}
// uplink_close_project closes the project.
//
//export uplink_close_project
func uplink_close_project(project *C.UplinkProject) *C.UplinkError {
if project == nil {
return nil
}
proj, ok := universe.Get(project._handle).(*Project)
if !ok {
return mallocError(ErrInvalidHandle.New("project"))
}
proj.cancel()
return mallocError(proj.Close())
}
// uplink_revoke_access revokes the API key embedded in the provided access grant.
//
//export uplink_revoke_access
func uplink_revoke_access(project *C.UplinkProject, access *C.UplinkAccess) *C.UplinkError {
if project == nil {
return mallocError(ErrNull.New("project"))
}
if access == nil {
return mallocError(ErrNull.New("access"))
}
proj, ok := universe.Get(project._handle).(*Project)
if !ok {
return mallocError(ErrInvalidHandle.New("project"))
}
acc, ok := universe.Get(access._handle).(*Access)
if !ok {
return mallocError(ErrInvalidHandle.New("access"))
}
scope := rootScope("")
return mallocError(proj.RevokeAccess(scope.ctx, acc.Access))
}
// uplink_free_project_result frees any associated resources.
//
//export uplink_free_project_result
func uplink_free_project_result(result C.UplinkProjectResult) {
uplink_free_error(result.error)
freeProject(result.project)
}
// freeProject closes the project and frees any associated resources.
func freeProject(project *C.UplinkProject) {
if project == nil {
return
}
defer C.free(unsafe.Pointer(project))
defer universe.Del(project._handle)
proj, ok := universe.Get(project._handle).(*Project)
if !ok {
return
}
proj.cancel()
// in case we haven't already closed the project
_ = proj.Close()
// TODO: log error when we didn't close manually and the close returns an error
}