|
| 1 | +package godo |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | +) |
| 8 | + |
| 9 | +const nfsBasePath = "v2/nfs" |
| 10 | + |
| 11 | +type NfsService interface { |
| 12 | + // List retrieves a list of NFS shares with optional filtering via ListOptions and region |
| 13 | + List(context.Context, *ListOptions, string) ([]*Nfs, *Response, error) |
| 14 | + // Create creates a new NFS share with the provided configuration |
| 15 | + Create(context.Context, *NfsCreateRequest) (*Nfs, *Response, error) |
| 16 | + // Delete removes an NFS share by its ID and region |
| 17 | + Delete(context.Context, string, string) (*Response, error) |
| 18 | + // Get retrieves a specific NFS share by its ID and region |
| 19 | + Get(context.Context, string, string) (*Nfs, *Response, error) |
| 20 | +} |
| 21 | + |
| 22 | +// NfsServiceOp handles communication with the NFS related methods of the |
| 23 | +// DigitalOcean API. |
| 24 | +type NfsServiceOp struct { |
| 25 | + client *Client |
| 26 | +} |
| 27 | + |
| 28 | +var _ NfsService = &NfsServiceOp{} |
| 29 | + |
| 30 | +// Nfs represents a DigitalOcean NFS share |
| 31 | +type Nfs struct { |
| 32 | + // ID is the unique identifier for the NFS share |
| 33 | + ID string `json:"id"` |
| 34 | + // Name is the human-readable name for the NFS share |
| 35 | + Name string `json:"name"` |
| 36 | + // SizeGib is the size of the NFS share in gibibytes |
| 37 | + SizeGib int `json:"size_gib"` |
| 38 | + // Region is the datacenter region where the NFS share is located |
| 39 | + Region string `json:"region"` |
| 40 | + // Status represents the current state of the NFS share |
| 41 | + Status string `json:"status"` |
| 42 | + // CreatedAt is the timestamp when the NFS share was created |
| 43 | + CreatedAt string `json:"created_at"` |
| 44 | + // VpcIDs is a list of VPC IDs that have access to the NFS share |
| 45 | + VpcIDs []string `json:"vpc_ids"` |
| 46 | +} |
| 47 | + |
| 48 | +// NfsCreateRequest represents a request to create an NFS share. |
| 49 | +type NfsCreateRequest struct { |
| 50 | + Name string `json:"name"` |
| 51 | + SizeGib int `json:"size_gib"` |
| 52 | + Region string `json:"region"` |
| 53 | + VpcIDs []string `json:"vpc_ids,omitempty"` |
| 54 | +} |
| 55 | + |
| 56 | +// nfsRoot represents a response from the DigitalOcean API |
| 57 | +type nfsRoot struct { |
| 58 | + Share *Nfs `json:"share"` |
| 59 | +} |
| 60 | + |
| 61 | +// nfsListRoot represents a response from the DigitalOcean API |
| 62 | +type nfsListRoot struct { |
| 63 | + Shares []*Nfs `json:"shares,omitempty"` |
| 64 | + Links *Links `json:"links,omitempty"` |
| 65 | + Meta *Meta `json:"meta"` |
| 66 | +} |
| 67 | + |
| 68 | +// nfsOptions represents the query param options for NFS operations |
| 69 | +type nfsOptions struct { |
| 70 | + Region string `url:"region"` |
| 71 | +} |
| 72 | + |
| 73 | +// Create creates a new NFS share. |
| 74 | +func (s *NfsServiceOp) Create(ctx context.Context, createRequest *NfsCreateRequest) (*Nfs, *Response, error) { |
| 75 | + if createRequest == nil { |
| 76 | + return nil, nil, NewArgError("createRequest", "cannot be nil") |
| 77 | + } |
| 78 | + |
| 79 | + if createRequest.SizeGib < 50 { |
| 80 | + return nil, nil, NewArgError("size_gib", "it cannot be less than 50Gib") |
| 81 | + } |
| 82 | + |
| 83 | + req, err := s.client.NewRequest(ctx, http.MethodPost, nfsBasePath, createRequest) |
| 84 | + if err != nil { |
| 85 | + return nil, nil, err |
| 86 | + } |
| 87 | + |
| 88 | + root := new(nfsRoot) |
| 89 | + resp, err := s.client.Do(ctx, req, root) |
| 90 | + if err != nil { |
| 91 | + return nil, resp, err |
| 92 | + } |
| 93 | + |
| 94 | + return root.Share, resp, nil |
| 95 | +} |
| 96 | + |
| 97 | +// Get retrieves an NFS share by ID and region. |
| 98 | +func (s *NfsServiceOp) Get(ctx context.Context, id string, region string) (*Nfs, *Response, error) { |
| 99 | + if id == "" { |
| 100 | + return nil, nil, NewArgError("id", "cannot be empty") |
| 101 | + } |
| 102 | + if region == "" { |
| 103 | + return nil, nil, NewArgError("region", "cannot be empty") |
| 104 | + } |
| 105 | + |
| 106 | + path := fmt.Sprintf("%s/%s", nfsBasePath, id) |
| 107 | + |
| 108 | + getOpts := &nfsOptions{Region: region} |
| 109 | + path, err := addOptions(path, getOpts) |
| 110 | + if err != nil { |
| 111 | + return nil, nil, err |
| 112 | + } |
| 113 | + |
| 114 | + req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil) |
| 115 | + if err != nil { |
| 116 | + return nil, nil, err |
| 117 | + } |
| 118 | + |
| 119 | + root := new(nfsRoot) |
| 120 | + resp, err := s.client.Do(ctx, req, root) |
| 121 | + if err != nil { |
| 122 | + return nil, resp, err |
| 123 | + } |
| 124 | + |
| 125 | + return root.Share, resp, nil |
| 126 | +} |
| 127 | + |
| 128 | +// List returns a list of NFS shares. |
| 129 | +func (s *NfsServiceOp) List(ctx context.Context, opts *ListOptions, region string) ([]*Nfs, *Response, error) { |
| 130 | + if region == "" { |
| 131 | + return nil, nil, NewArgError("region", "cannot be empty") |
| 132 | + } |
| 133 | + |
| 134 | + path, err := addOptions(nfsBasePath, opts) |
| 135 | + if err != nil { |
| 136 | + return nil, nil, err |
| 137 | + } |
| 138 | + |
| 139 | + listOpts := &nfsOptions{Region: region} |
| 140 | + path, err = addOptions(path, listOpts) |
| 141 | + if err != nil { |
| 142 | + return nil, nil, err |
| 143 | + } |
| 144 | + |
| 145 | + req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil) |
| 146 | + if err != nil { |
| 147 | + return nil, nil, err |
| 148 | + } |
| 149 | + |
| 150 | + root := new(nfsListRoot) |
| 151 | + resp, err := s.client.Do(ctx, req, root) |
| 152 | + if err != nil { |
| 153 | + return nil, resp, err |
| 154 | + } |
| 155 | + |
| 156 | + if root.Links != nil { |
| 157 | + resp.Links = root.Links |
| 158 | + } |
| 159 | + if root.Meta != nil { |
| 160 | + resp.Meta = root.Meta |
| 161 | + } |
| 162 | + |
| 163 | + return root.Shares, resp, nil |
| 164 | +} |
| 165 | + |
| 166 | +// Delete deletes an NFS share by ID and region. |
| 167 | +func (s *NfsServiceOp) Delete(ctx context.Context, id string, region string) (*Response, error) { |
| 168 | + if id == "" { |
| 169 | + return nil, NewArgError("id", "cannot be empty") |
| 170 | + } |
| 171 | + if region == "" { |
| 172 | + return nil, NewArgError("region", "cannot be empty") |
| 173 | + } |
| 174 | + |
| 175 | + path := fmt.Sprintf("%s/%s", nfsBasePath, id) |
| 176 | + |
| 177 | + deleteOpts := &nfsOptions{Region: region} |
| 178 | + path, err := addOptions(path, deleteOpts) |
| 179 | + if err != nil { |
| 180 | + return nil, err |
| 181 | + } |
| 182 | + |
| 183 | + req, err := s.client.NewRequest(ctx, http.MethodDelete, path, nil) |
| 184 | + if err != nil { |
| 185 | + return nil, err |
| 186 | + } |
| 187 | + |
| 188 | + resp, err := s.client.Do(ctx, req, nil) |
| 189 | + if err != nil { |
| 190 | + return resp, err |
| 191 | + } |
| 192 | + |
| 193 | + return resp, nil |
| 194 | +} |
0 commit comments