That's not what the NSA did, and US legal code applies to US citizens, not foreign ones. Also, if the NSA is operating within boundaries set by other laws like the PATRIOT Act, which they were, then they're in the clear.
Blame the law and the politicians for poor oversight, the NSA is just a bureaucracy told to go do something without sufficient guidelines and oversight.
Several problems with this.
First, the NSA has swept up plenty of information about US citizens, e.g. in requesting phone records in bulk, and we only have their word that they're only interested in foreigners. Not that that legally justifies sweeping up people you're not allowed to look at without a warrant.
Second, James Sensenbrenner, the Republican main sponsor of the PATRIOT Act, has said that the NSA is far overreaching its authorization under the Act. It's very possible that the agency's interpretation of the Act is far out of bounds with its congressional intent (or possibly even its language).
Third, even if they did not go beyond the bounds of the law, the question of whether the law itself is constitutional isn't a settled one. Many of the provisions have been untested due to difficulty in claiming standing due to government secrecy about what they do with the information they've collected.
Fourth, the question of legality isn't the only one. There's also the question of morality, of hypocrisy, and of the dangers inherent to information asymmetry between the government and the people.
Fifth, absolving the actions of an agency (or any individual) who uses a lack of clear guidelines as an excuse to go as far into bad behavior as they think they can get away with is a terrible idea. It's the same sort of mentality that says, "Well, it wasn't illegal back then to rape your wife, so how could it have been wrong?" You wouldn't raise kids that way, and you shouldn't expect your government to behave responsibly if they know they can get away with anything as long as it hasn't been written down that they shouldn't.