From a746808b0ecbe0ed470e3a060697ec053ac8ee62 Mon Sep 17 00:00:00 2001 From: zengyingzhe <52645009+zengyingzhe@users.noreply.github.com> Date: Sat, 11 Jan 2020 17:23:52 +0800 Subject: [PATCH] enhance the volume detachment process --- contrib/drivers/huawei/oceanstor/client.go | 63 ++++++++++++++++--- contrib/drivers/huawei/oceanstor/constants.go | 21 +++++-- contrib/drivers/huawei/oceanstor/oceanstor.go | 17 +++-- 3 files changed, 81 insertions(+), 20 deletions(-) diff --git a/contrib/drivers/huawei/oceanstor/client.go b/contrib/drivers/huawei/oceanstor/client.go index 1320c087e..edea1965a 100644 --- a/contrib/drivers/huawei/oceanstor/client.go +++ b/contrib/drivers/huawei/oceanstor/client.go @@ -980,9 +980,14 @@ func (c *OceanStorClient) GetHostLunId(hostId, lunId string) (int, error) { func (c *OceanStorClient) RemoveLunFromLunGroup(lunGrpId, lunId string) error { url := fmt.Sprintf("/lungroup/associate?ID=%s&ASSOCIATEOBJTYPE=11&ASSOCIATEOBJID=%s", lunGrpId, lunId) if err := c.request("DELETE", url, nil, nil); err != nil { + if c.checkErrorCode(err, ErrorObjectUnavailable) { + return nil + } + log.Errorf("Remove lun %s from lun group %s failed, %v", lunId, lunGrpId, err) return err } + log.Infof("Remove lun %s from lun group %s success", lunId, lunGrpId) return nil } @@ -992,16 +997,23 @@ func (c *OceanStorClient) RemoveLunGroupFromMappingView(viewId, lunGrpId string) log.Infof("Lun group %s has already been removed from mapping view %s", lunGrpId, viewId) return nil } + url := "/mappingview/REMOVE_ASSOCIATE" data := map[string]interface{}{ "ASSOCIATEOBJTYPE": ObjectTypeLunGroup, "ASSOCIATEOBJID": lunGrpId, "TYPE": ObjectTypeMappingView, "ID": viewId} + if err := c.request("PUT", url, data, nil); err != nil { + if c.checkErrorCode(err, ErrorLunGroupNotInMappingView) { + return nil + } + log.Errorf("Remove lun group %s from mapping view %s failed", lunGrpId, viewId) return err } + log.Infof("Remove lun group %s from mapping view %s success", lunGrpId, viewId) return nil } @@ -1011,58 +1023,93 @@ func (c *OceanStorClient) RemoveHostGroupFromMappingView(viewId, hostGrpId strin log.Infof("Host group %s has already been removed from mapping view %s", hostGrpId, viewId) return nil } + url := "/mappingview/REMOVE_ASSOCIATE" data := map[string]interface{}{ "ASSOCIATEOBJTYPE": ObjectTypeHostGroup, "ASSOCIATEOBJID": hostGrpId, "TYPE": ObjectTypeMappingView, "ID": viewId} + if err := c.request("PUT", url, data, nil); err != nil { + if c.checkErrorCode(err, ErrorHostGroupNotInMappingView) { + return nil + } + log.Errorf("Remove host group %s from mapping view %s failed", hostGrpId, viewId) return err } + log.Infof("Remove host group %s from mapping view %s success", hostGrpId, viewId) return nil } func (c *OceanStorClient) RemoveHostFromHostGroup(hostGrpId, hostId string) error { - url := fmt.Sprintf("/host/associate?TYPE=14&ID=%s&ASSOCIATEOBJTYPE=21&ASSOCIATEOBJID=%s", hostGrpId, hostId) if err := c.request("DELETE", url, nil, nil); err != nil { + if c.checkErrorCode(err, ErrorHostNotInHostGroup) { + return nil + } + log.Errorf("Remove host %s from host group %s failed", hostId, hostGrpId) return err } + log.Infof("Remove host %s from host group %s success", hostId, hostGrpId) return nil } func (c *OceanStorClient) RemoveIscsiFromHost(initiator string) error { - url := "/iscsi_initiator/remove_iscsi_from_host" data := map[string]interface{}{"TYPE": ObjectTypeIscsiInitiator, "ID": initiator} if err := c.request("PUT", url, data, nil); err != nil { + if c.checkErrorCode(err, ErrorInitiatorNotInHost) { + return nil + } + log.Errorf("Remove initiator %s failed", initiator) return err } + log.Infof("Remove initiator %s success", initiator) return nil } func (c *OceanStorClient) DeleteHostGroup(id string) error { - return c.request("DELETE", "/hostgroup/"+id, nil, nil) + err := c.request("DELETE", "/hostgroup/"+id, nil, nil) + if err != nil && c.checkErrorCode(err, ErrorHostGroupNotExist) { + return nil + } + + return err } func (c *OceanStorClient) DeleteLunGroup(id string) error { - return c.request("DELETE", "/LUNGroup/"+id, nil, nil) + err := c.request("DELETE", "/LUNGroup/"+id, nil, nil) + if err != nil && c.checkErrorCode(err, ErrorObjectUnavailable) { + return nil + } + + return err } func (c *OceanStorClient) DeleteHost(id string) error { - return c.request("DELETE", "/host/"+id, nil, nil) + err := c.request("DELETE", "/host/"+id, nil, nil) + if err != nil && c.checkErrorCode(err, ErrorHostNotExist) { + return nil + } + + return err } func (c *OceanStorClient) DeleteMappingView(id string) error { - return c.request("DELETE", "/mappingview/"+id, nil, nil) + err := c.request("DELETE", "/mappingview/"+id, nil, nil) + if err != nil && c.checkErrorCode(err, ErrorMappingViewNotExist) { + return nil + } + + return err } func (c *OceanStorClient) GetArrayInfo() (*System, error) { @@ -1262,10 +1309,6 @@ func (c *OceanStorClient) IsHostAssociatedToHostgroup(hostId string) (bool, erro return false, nil } -func (c *OceanStorClient) RemoveHost(hostId string) error { - return c.request("DELETE", fmt.Sprintf("/host/%s", hostId), nil, nil) -} - func (c *OceanStorClient) AddFCPortTohost(hostId string, wwn string) error { url := fmt.Sprintf("/fc_initiator/%s", wwn) data := map[string]interface{}{ diff --git a/contrib/drivers/huawei/oceanstor/constants.go b/contrib/drivers/huawei/oceanstor/constants.go index 2644b3f7e..6303d37a4 100644 --- a/contrib/drivers/huawei/oceanstor/constants.go +++ b/contrib/drivers/huawei/oceanstor/constants.go @@ -53,11 +53,22 @@ const ( // Error Code const ( - ErrorConnectToServer = -403 - ErrorUnauthorizedToServer = -401 - ErrorObjectUnavailable = 1077948996 - ErrorHostGroupNotExist = 1077937500 - ErrorObjectNameAlreadyExist = 1077948993 + ErrorConnectToServer = -403 + ErrorUnauthorizedToServer = -401 + ErrorObjectUnavailable = 1077948996 + ErrorHostGroupNotExist = 1077937500 + ErrorObjectNameAlreadyExist = 1077948993 + ErrorHostAlreadyInHostGroup = 1077937501 + ErrorObjectIDNotUnique = 1077948997 + ErrorHostGroupAlreadyInMappingView = 1073804556 + ErrorLunGroupAlreadyInMappingView = 1073804560 + ErrorLunNotExist = 1077936859 + ErrorLunGroupNotInMappingView = 1073804554 + ErrorHostGroupNotInMappingView = 1073804552 + ErrorHostNotInHostGroup = 1073745412 + ErrorHostNotExist = 1077937498 + ErrorMappingViewNotExist = 1077951819 + ErrorInitiatorNotInHost = 1077950342 ) // misc diff --git a/contrib/drivers/huawei/oceanstor/oceanstor.go b/contrib/drivers/huawei/oceanstor/oceanstor.go index 2b57b371c..0663894e5 100644 --- a/contrib/drivers/huawei/oceanstor/oceanstor.go +++ b/contrib/drivers/huawei/oceanstor/oceanstor.go @@ -619,7 +619,7 @@ func (d *Driver) connectFCUseNoSwitch(opt *pb.CreateVolumeAttachmentOpts, initia } if wwnsInHost == nil && iqnsInHost == nil && flag == false { - if err = d.client.RemoveHost(hostId); err != nil { + if err = d.client.DeleteHost(hostId); err != nil { return nil, nil, err } } @@ -739,19 +739,26 @@ func (d *Driver) deleteZoneAndRemoveFCInitiators(wwns []string, hostId, hostGrpI func (d *Driver) getMappedInfo(hostName string) (string, string, string, string, error) { hostId, err := d.client.GetHostIdByName(hostName) if err != nil { + if IsNotFoundError(err) { + log.Warningf("host(%s) has been removed already, ignore it.", hostName) + return "", "", "", "", nil + } + return "", "", "", "", err } lunGrpId, err := d.client.FindLunGroup(PrefixLunGroup + hostId) - if err != nil { + if err != nil && !IsNotFoundError(err) { return "", "", "", "", err } + hostGrpId, err := d.client.FindHostGroup(PrefixHostGroup + hostId) - if err != nil { + if err != nil && !IsNotFoundError(err) { return "", "", "", "", err } + viewId, err := d.client.FindMappingView(PrefixMappingView + hostId) - if err != nil { + if err != nil && !IsNotFoundError(err) { return "", "", "", "", err } @@ -799,7 +806,7 @@ func (d *Driver) clearHostRelatedResource(lunGrpId, viewId, hostId, hostGrpId st return err } if !flag { - if err := d.client.RemoveHost(hostId); err != nil { + if err := d.client.DeleteHost(hostId); err != nil { return err } }