vitess.io/vitess@v0.16.2/go/vt/topo/external_vitess_cluster.go (about) 1 /* 2 Copyright 2019 The Vitess Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package topo 18 19 import ( 20 "context" 21 "path" 22 23 "google.golang.org/protobuf/proto" 24 25 "vitess.io/vitess/go/event" 26 topodatapb "vitess.io/vitess/go/vt/proto/topodata" 27 "vitess.io/vitess/go/vt/topo/events" 28 "vitess.io/vitess/go/vt/vterrors" 29 ) 30 31 // ExternalVitessClusterInfo is a meta struct that contains metadata to give the 32 // data more context and convenience. This is the main way we interact 33 // with a vitess cluster stored in the topo. 34 type ExternalVitessClusterInfo struct { 35 ClusterName string 36 version Version 37 *topodatapb.ExternalVitessCluster 38 } 39 40 // GetExternalVitessClusterDir returns node path containing external vitess clusters 41 func GetExternalVitessClusterDir() string { 42 return path.Join(ExternalClustersFile, ExternalClusterVitess) 43 } 44 45 // GetExternalVitessClusterPath returns node path containing external clusters 46 func GetExternalVitessClusterPath(clusterName string) string { 47 return path.Join(GetExternalVitessClusterDir(), clusterName) 48 } 49 50 // CreateExternalVitessCluster creates a topo record for the passed vitess cluster 51 func (ts *Server) CreateExternalVitessCluster(ctx context.Context, clusterName string, value *topodatapb.ExternalVitessCluster) error { 52 data, err := proto.Marshal(value) 53 if err != nil { 54 return err 55 } 56 57 if _, err := ts.globalCell.Create(ctx, GetExternalVitessClusterPath(clusterName), data); err != nil { 58 return err 59 } 60 61 event.Dispatch(&events.ExternalVitessClusterChange{ 62 ClusterName: clusterName, 63 ExternalVitessCluster: value, 64 Status: "created", 65 }) 66 return nil 67 } 68 69 // GetExternalVitessCluster returns a topo record for the named vitess cluster 70 func (ts *Server) GetExternalVitessCluster(ctx context.Context, clusterName string) (*ExternalVitessClusterInfo, error) { 71 data, version, err := ts.globalCell.Get(ctx, GetExternalVitessClusterPath(clusterName)) 72 switch { 73 case IsErrType(err, NoNode): 74 return nil, nil 75 case err == nil: 76 default: 77 return nil, err 78 } 79 vc := &topodatapb.ExternalVitessCluster{} 80 if err = proto.Unmarshal(data, vc); err != nil { 81 return nil, vterrors.Wrap(err, "bad vitess cluster data") 82 } 83 84 return &ExternalVitessClusterInfo{ 85 ClusterName: clusterName, 86 version: version, 87 ExternalVitessCluster: vc, 88 }, nil 89 } 90 91 // UpdateExternalVitessCluster updates the topo record for the named vitess cluster 92 func (ts *Server) UpdateExternalVitessCluster(ctx context.Context, vc *ExternalVitessClusterInfo) error { 93 //FIXME: check for cluster lock 94 data, err := proto.Marshal(vc.ExternalVitessCluster) 95 if err != nil { 96 return err 97 } 98 version, err := ts.globalCell.Update(ctx, GetExternalVitessClusterPath(vc.ClusterName), data, vc.version) 99 if err != nil { 100 return err 101 } 102 vc.version = version 103 104 event.Dispatch(&events.ExternalVitessClusterChange{ 105 ClusterName: vc.ClusterName, 106 ExternalVitessCluster: vc.ExternalVitessCluster, 107 Status: "updated", 108 }) 109 return nil 110 } 111 112 // DeleteExternalVitessCluster deletes the topo record for the named vitess cluster 113 func (ts *Server) DeleteExternalVitessCluster(ctx context.Context, clusterName string) error { 114 if err := ts.globalCell.Delete(ctx, GetExternalVitessClusterPath(clusterName), nil); err != nil { 115 return err 116 } 117 118 event.Dispatch(&events.ExternalVitessClusterChange{ 119 ClusterName: clusterName, 120 ExternalVitessCluster: nil, 121 Status: "deleted", 122 }) 123 return nil 124 } 125 126 // GetExternalVitessClusters returns the list of external vitess clusters in the topology. 127 func (ts *Server) GetExternalVitessClusters(ctx context.Context) ([]string, error) { 128 children, err := ts.globalCell.ListDir(ctx, GetExternalVitessClusterDir(), false /*full*/) 129 switch { 130 case err == nil: 131 return DirEntriesToStringArray(children), nil 132 case IsErrType(err, NoNode): 133 return nil, nil 134 default: 135 return nil, err 136 } 137 }