|
1 | 1 | package iscsi
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "encoding/json" |
4 | 5 | "fmt"
|
5 | 6 | "io"
|
6 | 7 | "io/ioutil"
|
@@ -33,20 +34,18 @@ type iscsiSession struct {
|
33 | 34 |
|
34 | 35 | //Connector provides a struct to hold all of the needed parameters to make our iscsi connection
|
35 | 36 | type Connector struct {
|
36 |
| - VolumeName string |
37 |
| - TargetIqn string |
38 |
| - TargetPortals []string |
39 |
| - Port string |
40 |
| - Lun int32 |
41 |
| - AuthType string |
42 |
| - DiscoverySecrets Secrets |
43 |
| - SessionSecrets Secrets |
44 |
| - Interface string |
45 |
| - Multipath bool |
46 |
| - // Number of time we try to obtain a device path, with CheckInterval seconds between each attempt |
47 |
| - RetryCount int32 |
48 |
| - // Time (in seconds) between login attempts |
49 |
| - CheckInterval int32 |
| 37 | + VolumeName string `json:"volume_name"` |
| 38 | + TargetIqn string `json:"target_iqn"` |
| 39 | + TargetPortals []string `json:"target_portals"` |
| 40 | + Port string `json:"port"` |
| 41 | + Lun int32 `json:"lun"` |
| 42 | + AuthType string `json:"auth_type"` |
| 43 | + DiscoverySecrets Secrets `json:"discovery_secrets"` |
| 44 | + SessionSecrets Secrets `json:"session_secrets"` |
| 45 | + Interface string `json:"interface"` |
| 46 | + Multipath bool `json:"multipath"` |
| 47 | + RetryCount int32 `json:"retry_count"` |
| 48 | + CheckInterval int32 `json:"check_interval"` |
50 | 49 | }
|
51 | 50 |
|
52 | 51 | func init() {
|
@@ -324,3 +323,36 @@ func Disconnect(tgtIqn string, portals []string) error {
|
324 | 323 | err = DeleteDBEntry(tgtIqn)
|
325 | 324 | return err
|
326 | 325 | }
|
| 326 | + |
| 327 | +// PersistConnector persists the provided Connector to the specified file (ie /var/lib/pfile/myConnector.json) |
| 328 | +func PersistConnector(c *Connector, filePath string) error { |
| 329 | + //file := path.Join("mnt", c.VolumeName+".json") |
| 330 | + f, err := os.Create(filePath) |
| 331 | + if err != nil { |
| 332 | + return fmt.Errorf("error creating iscsi persistence file %s: %s", filePath, err) |
| 333 | + } |
| 334 | + defer f.Close() |
| 335 | + encoder := json.NewEncoder(f) |
| 336 | + if err = encoder.Encode(c); err != nil { |
| 337 | + return fmt.Errorf("error encoding connector: %v", err) |
| 338 | + } |
| 339 | + return nil |
| 340 | + |
| 341 | +} |
| 342 | + |
| 343 | +// GetConnectorFromFile attempts to create a Connector using the specified json file (ie /var/lib/pfile/myConnector.json) |
| 344 | +func GetConnectorFromFile(filePath string) (*Connector, error) { |
| 345 | + f, err := ioutil.ReadFile(filePath) |
| 346 | + if err != nil { |
| 347 | + return &Connector{}, err |
| 348 | + |
| 349 | + } |
| 350 | + data := Connector{} |
| 351 | + err = json.Unmarshal([]byte(f), &data) |
| 352 | + if err != nil { |
| 353 | + return &Connector{}, err |
| 354 | + } |
| 355 | + |
| 356 | + return &data, nil |
| 357 | + |
| 358 | +} |
0 commit comments