// SetConfig implements SetConfig function of the Service interface. // Returns an error if cannot connect to the data store func (topology *TopologySvc) SetConfig(config common.ServiceConfig) error { topology.config = config dcMap := config.ServiceSpecific["datacenter"].(map[string]interface{}) dc := common.Datacenter{} dc.IpVersion = uint(dcMap["ip_version"].(float64)) if dc.IpVersion != 4 { return common.NewError("Only IPv4 is currently supported.") } dc.Cidr = dcMap["cidr"].(string) _, ipNet, err := net.ParseCIDR(dc.Cidr) if err != nil { return err } prefixBits, _ := ipNet.Mask.Size() dc.PrefixBits = uint(prefixBits) dc.PortBits = uint(dcMap["host_bits"].(float64)) dc.TenantBits = uint(dcMap["tenant_bits"].(float64)) dc.SegmentBits = uint(dcMap["segment_bits"].(float64)) dc.EndpointBits = uint(dcMap["endpoint_bits"].(float64)) dc.EndpointSpaceBits = uint(dcMap["endpoint_space_bits"].(float64)) if dc.EndpointBits == 0 { return common.NewError("Endpoint bits may not be 0") } bitSum := dc.PrefixBits + dc.PortBits + dc.TenantBits + dc.SegmentBits + dc.EndpointBits + dc.EndpointSpaceBits if bitSum != 32 { bitSumStr := fmt.Sprintf("%s+%d+%d+%d+%d+%d", dc.Cidr, dc.PortBits, dc.TenantBits, dc.SegmentBits, dc.EndpointBits, dc.EndpointSpaceBits) return common.NewError("Sum of prefix, port, tenant, segment, endpoint and endpoint space bits must be exactly 32, but it is %s=%d", bitSumStr, bitSum) } // TODO this should have worked but it doesn't... // err := mapstructure.Decode(dcMap, &dc) // if err != nil { // return err // } log.Printf("Datacenter information: was %s, decoded to %+v\n", dcMap, dc) topology.datacenter = &dc storeConfig := config.ServiceSpecific["store"].(map[string]interface{}) topology.store = topoStore{} topology.store.ServiceStore = &topology.store return topology.store.SetConfig(storeConfig) }
// SetConfig implements SetConfig function of the Service interface. // Returns an error if cannot connect to the data store func (topology *TopologySvc) SetConfig(config common.ServiceConfig) error { log.Println(config) topology.config = config dcMap := config.ServiceSpecific["datacenter"].(map[string]interface{}) dc := common.Datacenter{} dc.IpVersion = uint(dcMap["ip_version"].(float64)) dc.Cidr = dcMap["cidr"].(string) _, ipNet, err := net.ParseCIDR(dc.Cidr) if err != nil { return err } prefixBits, _ := ipNet.Mask.Size() dc.PrefixBits = uint(prefixBits) dc.PortBits = uint(dcMap["host_bits"].(float64)) dc.TenantBits = uint(dcMap["tenant_bits"].(float64)) dc.SegmentBits = uint(dcMap["segment_bits"].(float64)) dc.EndpointBits = uint(dcMap["endpoint_bits"].(float64)) dc.EndpointSpaceBits = uint(dcMap["endpoint_space_bits"].(float64)) // TODO this should have worked but it doesn't... // err := mapstructure.Decode(dcMap, &dc) // if err != nil { // return err // } log.Printf("Datacenter information: was %s, decoded to %s\n", dcMap, dc) topology.datacenter = &dc storeConfig := config.ServiceSpecific["store"].(map[string]interface{}) storeType := strings.ToLower(storeConfig["type"].(string)) switch storeType { case "mysql": topology.store = &mysqlStore{} case "mock": topology.store = &mockStore{} default: return errors.New("Unknown store type: " + storeType) } return topology.store.setConfig(storeConfig) }